RegEx = new RegExp(sourcevalue, "gi");
temp[i] = Selectors[i].cssText.replace(RegEx, targetvalue);
where sourcevalue = rgb(255,0,0) and targetvalue = #FFFFFF, but sourcevalue is not replaced by targetvalue, why?
Most likely because you did not escape the parenthesis properly:
var sourcevalue = "rgb\\(255,0,0\\)";
Parenthesis are used for grouping/back-referencing matches. If you want to match them literally, you have to escape them with \. Since sourcevalue is a string and \ is the escape character in a string as well, you have to escape the backslash to create a literal one.
Otherwise, the expression would match rgb255,0,0 but not rgb(255,0,0).
Because sourcevalue is a correct Javascript regular expression but it's not working the way you expected. There are special characters in regular expression like the () parentheses. They are used to group parts of regular expressions. To use them with their "literal" meaing you'll have to escape them using backslashes
var RegEx = /rgb\(255,0,0\)/gi;
Related
In the following text:
"123+456+789"
How can I replace all the "+" characters with %20?
The examples I have seen that use regular expressions end up treating the + as part of the rules used by the expression, so it seems regular expressions cannot be used.
You just need to escape the + first, which is done with a backslash:
const str = "123+456+789";
console.log(
str.replace(/\+/g, '%20')
);
The same thing is true for any character with a special meaning in a regular expression, like parentheses, *, ^, and so on - to match a literal character, put a backslash in front of it. (Same for the backslash itself - \\ matches a literal backslash)
You can use String#split() and Array#join() to remove all plusses and replace them with %20:
let plus = "123+456+789";
let percent = plus.split("+").join("%20");
console.log(percent)
You can achieve this by using str replace
str = document.getElementById("mystring").innerHTML;
res = str.replace("+", "%20");
<script>
var String = "1 Apple and 13 Oranges";
var regex = /[^\d]/g;
var regObj = new RegExp(regex);
document.write(String.replace(regObj,''));
</script>
And it works fine - return all the digits in the string.
However when I put quote marks around the regex like this:
var regex = "/[^\d]/g"; This doesn't work.
How can I turn a string to a working regex in this case?
Thanks
You can create regular expressions in two ways, using the regular expression literal notation, or RegExp constructor. It seems you have mixed up the two. :)
Here is the literal way:
var regex = /[^\d]/g;
In this case you don't have use quotes. / characters at the ends serve as the delimiters, and you specify the flags at the end.
Here is how to use the RegExp constructor, in which you pass the pattern and flags (optional) as string. When you use strings you have to escape any special characters inside it using a '\'.
Since the '\' (backslash) is a special character, you have to escape the backslash using another backslash if you use double quotes.
var regex = new RegExp("[^\\d]", "g");
Hope this makes sense.
As slash(\) has special meaning for strings (e.g. "\n","\t", etc...), you need to escape that simbol, when you are passing to regexp:
var regex = "[^\\d]";
Also expression flags (e.g. g,i,etc...) must be passed as separate parameter for RegExp.
So overall:
var regex = "[^\\d]";
var flags = "g";
var regObj = new RegExp(regex, flags);
I want to replace all symbols that aren't letters by -, but my code doesn't work :
$reg = '/[^a-zA-Z]+/g';
$txt = $txt.replace($reg, '-');
What am I doing wrong?
Regular Expressions in JavaScript are not strings.
reg = /[^a-z]+/gi;
txt = txt.replace(reg, '-');
You don't need to place quotes around them.
You need to un-quote the regex string so it's treated as a regular expression literal, so you get this:
$reg = /[^a-zA-Z]+/g;
$txt = $txt.replace($reg, '-');
Regular expressions in JavaScript don't need to be quoted as strings unless using the new Regexp() notation; in the above example, it is now a regular expression literal, which isn't treated as a string but a piece of regex to be used in .replace().
do not use quote on regex. Without quotes, they are RegEx object. With quotes they are just string.
Use,
$reg = /[^a-zA-Z]+/g;
Remove the quotes from around your regex.
If it is your intention for multiple non-alpha characters in a row to be replaced with a single hyphen your regex will then work. If you want multiple non-alpha characters to be replaced with multiple hyphens then you should also remove the + sign.
I've got this regular expression for validating phone numbers
^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.//-]|\([ 0-9.//-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\([ 0-9.//-]+\)))?$
I dugged it out from my C#/vb library and now i want to translate it into javascript. But it has syntax error (i suspect it is something due to the // characters). my attempt:
$IsPhone = function (input) {
var regex = new window.RegExp("^$|^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.//-]|\([ 0-9.//-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\([ 0-9.//-]+\)))?$", "");
return regex.test(input.trim());
};
alert($IsPhone("asd"));
Your problem has nothing to do with comments. You're just mixing up the two different ways of creating RegExp objects.
When you create a RegExp object in JavaScript code, you either write it as a string literal which you pass to a RegExp constructor, or as a regex literal. Because string literals support backslash-escape sequences like \n and \", any actual backslash in the string has to be escaped, too. So, whenever you need to escape a regex metacharacter like ( or +, you have to use two backslashes, like so:
var r0 = "^$|^(\\+?|(\\(\\+?[0-9]{1,3}\\))|)([ 0-9./-]|\\([ 0-9./-]+\\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9./-]|\\([ 0-9./-]+\\)))?$";
var regex0 = new RegExp(r0, "");
The forward-slash has no special meaning, either to regexes or strings. The only reason you ever have to escape forward-slashes is because they're used as the delimiter for regex literals. You use backslashes to escape the forward-slashes just like you do with regex metacharacters like \( or \+, or the backslash itself: \\. Here's the regex-literal version of your regex:
var regex1 = /^$|^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.\/-]|\([ 0-9.\/-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.\/-]|\([ 0-9.\/-]+\)))?$/;
from Errors translating regex from .NET to javascript
The backslash character in JavaScript
strings is an escape character, so the
backslashes you have in your string
are escaping the next character for
the string, not for the regular
expression. So right near the
beginning, in your "^(+?, the
backslash there just escapes the + for
the string (which it doesn't need),
and what the regexp sees is just a raw
+ with nothing to repeat. Hence the error.
Why this regex '^[0-9]+\.?[0-9]*$' match for 12.2 and 12,2 ?
jsFiddle
var dot = '12.2',
comma = '12,2',
regex = '^[0-9]+\.?[0-9]*$';
alert( dot.match(regex) );
alert( comma.match(regex) );
While it works on regexpal.com
Because the variable regex is a string the escape sequence \. is just ., which matches any character (except newline). If you change the definition of regex to use RegExp literal syntax or escape the escape character (\\.) then it will work as you expect.
var dot = '12.2'
, comma = '12,2'
, regex = /^[0-9]+\.?[0-9]*$/;
// or '^[0-9]+\\.?[0-9]*$'
alert(dot.match(regex));
alert(comma.match(regex));
Are you sure you don't need to escape the back-slash? It is in a string, you know...
regex = /^[0-9]+\.?[0-9]*$/
or
regex = "^[0-9]+\\.?[0-9]*$"
Actually, I'd recommend that you write it this way:
regex = /^\d+(\.\d+)?$/
Since you write your regex in a string, you need to escape the slash.
regex = '^[0-9]+\\.?[0-9]*$';
Your regex should be
regex = /^[0-9]+\.?[0-9]*$/;
Consult https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/regexp for proper syntax.