This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 4 years ago.
Possible Duplicate:
Escape string for use in Javascript regex
I have a msg like this:
Max {0} chars allowed in {1}
And I have a function to create a message using the arguments passed as
for(var i = 0; i < agrs.length; i++){
reg = new RegExp('\{'+i+'\}', 'gi');
key = key.replace(reg,agrs[i])
}
The problem is that it's not able to take the param i to create the reg exp.
What's the way to achieve this?
Your regexp is /{0}/gi since you create it from a string. And it is not a valid expression.
You need to escape { in the regexp because it has a special meaning in the regexp syntax, so it should be:
new RegExp('\\{'+i+'\\}', 'gi');
which is /\\{0\\}/gi. You need to escape the escaping \\ in the string.
I would strongly encourage you to use the functional form of String.replace() to solve your problem, rather than trying to parametrize the regexp in a for-loop that iterates over {0},{1},etc.
In other words, rather than look for '{0}' or '{1}', just look for '{([0-9]+)}' (i.e. {}'s surrounding an arbitrary number, and pass a function to the replace() function to intelligently replace these expressions based on the number between the {}'s. This lets you use a RegExp literal which you can write as /{([0-9]+)}/ rather than mess around with escape characters in strings.
Something like this:
s='the song says {0} little {1} little {2} little-endians';
args=['zero','one','two'];
new_string = s.replace(/{([0-9]+)}/g, function(wholematch,firstmatch)
{return args[(+firstmatch)]; }
);
which yields
the song says zero little one little two little-endians
See this similar question.
edit: if you want to leave alone items that are greater than the length of your args list, make sure to sanity-check the parameter number:
s='the song says {0} little {1} little {2} little-endians,\n'+
' {3} little {4} little {5} little-endians';
args=['zero','one','two'];
new_string = s.replace(/{([0-9]+)}/g, function(wholematch,firstmatch)
{var i = +firstmatch; return i < args.length ? args[i] : wholematch;}
);
which yields
the song says zero little one little two little-endians,
{3} little {4} little {5} little-endians
I think your second line is supposed to be
reg = new RegExp('\{'+i+'\}', 'gi');
but I don't know if that's your only problem or not.
function escapeRegExp(str) {
return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
var re = new RegExp(escapeRegExp(str));
See: Escape string for use in Javascript regex
Related
I am doing a challenge on freeCodeCamp. I am passed an array with 2 strings, the instructions are to test to see if the letters in the second string are in the first string.
Here's what I have:
return /[arr\[1\]]/gi.test(arr[0]);
This passes all the tests except where it tries to match with a capital letter.
mutation(["hello", "Hello"]) should return true.
It's the only test that fails, I have tested my regex on regexr.com with:
/[Hello]/gi and it matches with 'hello'.
Yes, there are other ways to do it, but why does it fail when I pass the string into the regex from the array?
EDIT: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations
keep in mind that with this: return /[arr\[1\]]/gi.test(arr[0]) you are evaluating exactly this string "arr[1]". test() is a method of RegExp, then to add variables in a regex, or build the regex as string, you should use the RegExp constructor. Like the example below.
See this for browser compatibility of flags.
function mutation(str){
var r = new RegExp(str[0].toLowerCase(), "gi")
return r.test(str[1].toLowerCase());
}
console.log(mutation(["hello", "Hello"]))
The fact that your code passes the test for ["Mary", "Army"] shows that the problem is not one of case sensitivity. The only reason your code passes any of the tests is that /[arr\[1\]]/ looks for matches against the set of characters ar1[] which coincidentally happens to correctly match 8 of the 9 tests. Anyway the other - perhaps biggest - issue is that you are not testing all of the characters in arr[1] against arr[0]; if you run #Emeeus's answer it returns false positives for many of the tests. So, to test all of the characters in arr[1] against arr[0] you need something like this:
function mutation(arr) {
return arr[1].split('').reduce((t, c) => t && new RegExp(c, 'i').test(arr[0]), true);
}
let tests = [
['hello', 'hey'],
["hello", "Hello"],
["zyxwvutsrqponmlkjihgfedcba", "qrstu"],
["Mary", "Army"],
["Mary", "Aarmy"],
["Alien", "line"],
["floor", "for"],
["hello", "neo"],
["voodoo", "no"]
];
tests.map(arr => console.log(arr[0] + ", " + arr[1] + " => " + (mutation(arr) ? 'match' : 'no match')));
JavaScript has a special syntax for Regular Expressions. Those two lines are essentially the same:
return /[arr\[1\]]/gi.test(arr[0]);
return new RegExp('[arr\\[1\\]]', 'gi').test(arr[0]);
but what you probably want is this:
new RegExp('['+arr[1]+']', 'gi').test(arr[0]);
However, you should be careful since this approach does not work if it contains special characters such as '[' or ']'.
Whenever you have a javascript variable in a regular expression, you should construct a new RegExp object. Taken from your question, it should look like this
return new RegExp(arr[1], "gi").test(arr[0]);
As one hint on freeCodeCamp.org says, you can solve the problem easier if you transform the strings into arrays, using the spread operator. No need for regular expressions.
Instead of:
return /[arr\[1\]]/gi.test(arr[0]);
you can do:
return new RegEx(arr[1], gi);
Your code uses a character match ([ ]), not a string match, so it will match anything, that has those characters directly (That's why uppercase and lowercase differs, although you have specified 'i').
The new expression directly uses the string to match, not just the characters.
This question already has answers here:
JavaScript: RegExp constructor vs RegEx literal
(4 answers)
Closed 4 years ago.
I am trying to learn regular expressions in Javascript through this brilliant book "Eloquent Javascript" by Marijn Haverbeke. I am unable to figure out why some of these match and why some don't even though they seem fine. I don't know if I have misunderstood something or understood something partially. For example -
console.log(/'\d+'/.test("123"));
// This doesn't match
console.log(/'\d+'/.test("'123'"));
// This matches
let myRegEx = new RegExp("\d+");
console.log(myRegEx.test("123"));
//Doesn't match
console.log(myRegEx.test("'123'"));
//Doesn't match either
Also, why are '' required inside "" for the string to be matched?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
The correct code:
console.log(/\d+/.test("123"));
or
let myRegEx = new RegExp("\\d+");
console.log(myRegEx.test("123"));
You have to add \ for the new RegExpr because "\\d+" is a string interpreted as "\d+"
Also, why are '' required inside "" for the string to be matched?
They are not:
/\d+/ matches "A number, one or more that one time"
/'\d+'/ matches "A quote, then A number, one or more that one time, then a quote"
So:
/\d+/.test("123") === true
/'\d+'/.test("123") === false (because ' are not found)
/\d+/.test("'123'") === true (because numbers are found)
/'\d+'/.test("'123'") === true
\ is an escape character in string literals as well as regular expressions.
"\d+" is the same as "d+" so you are testing for 1 or more instances of the character d.
This question already has answers here:
Regular Expressions in JavaScript for URL Capture
(4 answers)
Closed 7 years ago.
I have the message string as follows.
string=052
I need to use regular expression not split.
I want to return everything past the equals. 052
This is what i tried and gives me id=null
var regex = '[^?string=]';
var id2 = mystring.match(regex);
I have tried online regex checkers and it looks like it matches all but the a
is there a better reg ex i should try? id should not equal null.
You're using String.match() incorrectly. Try this:
var regex = '^message=(.*)$';
var id = queryString.match(regex)[1];
.match() returns an array; the first element (at [0]) is the entire matched string, and the second element (at [1]) is the part that's matched in the (first) set of parentheses in the regex.
This question already has answers here:
Why does a RegExp with global flag give wrong results?
(7 answers)
Closed 7 years ago.
I am trying to validate strings with following pattern.
starts with 3 digits followed by hyphen followed by 2 digits and ends with 4 digits.
eg : 123-45-6789
I need to pass the pattern to a RegExp object as a string variable (I am getting the pattern value from a html element attribute ), hence I have escaped the RegExp with double slashes. To my surprise, I am seeing weird results.
var pattern = "^\\d{3}-\\d{2}-\\d{4}$";
var inputRegex = new RegExp(pattern, "g");
console.log(inputRegex.test('132-45-7899')); //True
console.log(inputRegex.test('132-45-7899')); //False
console.log(inputRegex.test('132-45-7899')); //True
console.log(inputRegex.test('132-45-7899')); //False
What am I doing wrong here? and what is the reason for this inconsistent results?
EDIT:
Now I understood why the results are inconsistent , but my actual problem remained unsolved.
HTML elem is like below
SCENARIO 1:
<input data-val-regexp="^\\d{3}-\\d{2}-\\d{4}$" id="ticketNumber">
var regexPattern = input.attr("data-val-regexp");
var inputRegex = new RegExp(regexPattern);
return inputRegex.test('123-45-6789'); // False
When I inspected, the inputRegex.source is "^\\d{3}-\\d{2}-\\d{4}$"
SCENARIO 2:
If I assign the regexPattern with string literals everything works fine.
var pattern = "^\\d{3}-\\d{2}-\\d{4}$";
var inputRegex = new RegExp(pattern);
console.log(inputRegex.test('132-45-7899')); //True
In the above case inputRegex source value is "^\d{3}-\d{2}-\d{4}$"
Why is the backslash escaped in the second scenario, where as it didn't in the first scenario? What needs to be done in order to make scenario 1 work?
Because you're creating the regular expression with the g flag, it remembers where it left off and starts matching there, instead of at the beginning, the next time you call test. See Why does Javascript's regex.exec() not always return the same value?.
My problem start with like-
var str='0|31|2|03|.....|4|2007'
str=str.replace(/[^|]\d*[^|]/,'5');
so the output becomes like:"0|5|2|03|....|4|2007" so it replaces 31->5
But this doesn't work for replacing other segments when i change code like this:
str=str.replace(/[^|]{2}\d*[^|]/,'6');
doesn't change 2->6.
What actually i am missing here.Any help?
I think a regular expression is a bad solution for that problem. I'd rather do something like this:
var str = '0|31|2|03|4|2007';
var segments = str.split("|");
segments[1] = "35";
segments[2] = "123";
Can't think of a good way to solve this with a regexp.
Here is a specific regex solution which replaces the number following the first | pipe symbol with the number 5:
var re = /^((?:\d+\|){1})\d+/;
return text.replace(re, '$15');
If you want to replace the digits following the third |, simply change the {1} portion of the regex to {3}
Here is a generalized function that will replace any given number slot (zero-based index), with a specified new number:
function replaceNthNumber(text, n, newnum) {
var re = new RegExp("^((?:\\d+\\|){"+ n +'})\\d+');
return text.replace(re, '$1'+ newnum);
}
Firstly, you don't have to escape | in the character set, because it doesn't have any special meaning in character sets.
Secondly, you don't put quantifiers in character sets.
And finally, to create a global matching expression, you have to use the g flag.
[^\|] means anything but a '|', so in your case it only matches a digit. So it will only match anything with 2 or more digits.
Second you should put the {2} outside of the []-brackets
I'm not sure what you want to achieve here.