This is probably a silly mistake, but I can't figure out why this isn't working
var patt = new RegExp("\s[A-Za-z0-9]");
var filtering = patt.test("1 1");
console.log(filtering);
I get false from filtering, but from my understanding filtering should be true
This:
var patt = new RegExp("\s[A-Za-z0-9]");
… creates the following regular expression:
/s[A-Za-z0-9]/
Note that there's no backslash (\) before the s, because a backslash in a string expression has to be escaped (with another backslash).
Fix that, and you'll get true as expected:
var patt = new RegExp("\\s[A-Za-z0-9]");
var filtering = patt.test("1 1");
console.log(filtering);
Related
I want to match 3 letters and 3 digits with regexp and exec. But I dont get any result. Any idea what is wrong? The code is:
var regnr = "This is the pattern to match: WBJ124";
var patt = new RegExp("^\b[a-zA-Z]{3}\d{3}\b*$");
var sequence = '';
var grps = patt.exec(regnr);
if(grps!=null){
sequence = grps[0];
}
sequence is empty, but I expect it to be WBJ124
Best Regards
You have few mistakes in your regex like using ^ and $ when you are matching a substring in a longer string. Also you are using RegExp object that accepts a string literal hence requires double escaping.
You can use:
var regnr = "This is the pattern to match: WBJ124";
var patt = /\b[a-zA-Z]{3}\d{3}\b/; // or new RegExp("\\b[a-zA-Z]{3}\\d{3}\\b")
var grps = patt.exec(regnr);
I am using the function match for a search engine, so whenever a user types a search-string I take that string and use the match function on an array containing country names, but it doesn't seem to work.
For example if I do :
var string = "algeria";
var res = string.match(/alge/g); //alge is what the user would have typed in the search bar
alert(res);
I get a string res = "alge": //thus verifying that alge exists in algeria
But if I do this, it returns null, why? and how can I make it work?
var regex = "/alge/g";
var string = "algeria";
var res = string.match(regex);
alert(res);
To make a regex from a string, you need to create a RegExp object:
var regex = new RegExp("alge", "g");
(Beware that unless your users will be typing actual regular expressions, you'll need to escape any characters that have special meaning within regular expressions - see Is there a RegExp.escape function in Javascript? for ways to do this.)
You don't need quotes around the regex:
var regex = /alge/g;
Remove the quotes around the regex.
var regex = /alge/g;
var string = "algeria";
var res = string.match(regex);
alert(res);
found the answer, the match function takes a regex object so have to do
var regex = new RegExp(string, "g");
var res = text.match(regex);
This works fine
I have the following javascript code:
var markdown = "I have \(x=1\) and \(y=2\) and even \[z=3\]"
var latexRegex = new RegExp("\\\[.*\\\]|\\\(.*\\\)");
var matches = latexRegex.exec(markdown);
alert(matches[0]);
matches has only matches[0] = "x=1 and y=2" and should be:
matches[0] = "\(x=1\)"
matches[1] = "\(y=2\)"
matches[2] = "\[z=3\]"
But this regex works fine in C#.
Any idea why this happens?
Thank You,
Miguel
Specify g flag to match multiple times.
Use String.match instead of RegExp.exec.
Using regular expression literal (/.../), you don't need to escape \.
* matches greedily. Use non-greedy version: *?
var markdown = "I have \(x=1\) and \(y=2\) and even \[z=3\]"
var latexRegex = /\[.*?\]|\(.*?\)/g;
var matches = markdown.match(latexRegex);
matches // => ["(x=1)", "(y=2)", "[z=3]"]
Try non-greedy: \\\[.*?\\\]|\\\(.*?\\\). You need to also use a loop if using the .exec() method like so:
var res, matches = [], string = 'I have \(x=1\) and \(y=2\) and even \[z=3\]';
var exp = new RegExp('\\\[.*?\\\]|\\\(.*?\\\)', 'g');
while (res = exp.exec(string)) {
matches.push(res[0]);
}
console.log(matches);
Try using the match function instead of the exec function. exec only returns the first string it finds, match returns them all, if the global flag is set.
var markdown = "I have \(x=1\) and \(y=2\) and even \[z=3\]";
var latexRegex = new RegExp("\\\[.*\\\]|\\\(.*\\\)", "g");
var matches = markdown.match(latexRegex);
alert(matches[0]);
alert(matches[1]);
If you don't want to get \(x=1\) and \(y=2\) as a match, you will need to use non-greedy operators (*?) instead of greedy operators (*). Your RegExp will become:
var latexRegex = new RegExp("\\\[.*?\\\]|\\\(.*?\\\)");
Recently I posted a question about time format conversion via regular expressions in JS.
Now I modified the code a bit.
function getHours(value) {
if (value == 0)
return 0;
var re = new RegExp("^(?=\d)((\d+)(h|:))?\s*((\d+)m?)?$", "g");
var myArray = re.exec(value);
var hours = 0;
var minutes = 0;
if (myArray != null) {
if (myArray[2] != null) {
hours = myArray[2];
}
if (myArray[5] != null) {
minutes = myArray[5];
}
}
return Number(hours) + Number(minutes) / 60;
}
The problem is that it returns a null value in myArray.
As I'm new to JS, I couldn't solve this problem. What am I doing wrong?
The problem is here
new RegExp("^(?=\d)((\d+)(h|:))?\s*((\d+)m?)?$", "g");
When you create a new Regular Expression through the constructor, you provide strings. In string literals, the backslash character (\) means ‘escape the next character’.
You have to escape those backslashes, so they won't escape their subsequent character. So the correct version is:
new RegExp("^(?=\\d)((\\d+)(h|:))?\\s*((\\d+)m?)?$", "g");
See this article on values, variables, and literals from MDN for more information on escaping characters in JavaScript.
Problem is in this line:
var re = new RegExp("^(?=\d)((\d+)(h|:))?\s*((\d+)m?)?$", "g");
Pls understand that RegExp class takes a String as argument to construct and you need to double escape \d and \s to be correctly interpreted by RegEx engine so \d should become \\d and \s should become \\sin your regex String like this:
var re = new RegExp("^(?=\\d)((\\d+)(h|:))?\\s*((\\d+)m?)?$", "g");
Note that you can also do:
var re = /^(?=\d)((\d+)(h|:))?\s*((\d+)m?)?$/g;
I changed exec call like this:
var myArray = (/^(?=\d)((\d+)(h|:))?\s*((\d+)m?)?$/g).exec(value);
And it worked! Can anyone explain the difference?
I'm sure this is an easy one, but I can't find it on the net.
This code:
var new_html = "foo and bar(arg)";
var bad_string = "bar(arg)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
sets bad_start to -1 (not found). If I remove the (arg), it runs as expected (bad_start == 8). Is there something I can do to make the (very handy) "new Regexp" syntax work, or do I have to find another way? This example is trivial, but in the real app it would be doing global search and replace, so I need the regex and the "g". Or do I?
TIA
Escape the brackets by double back slashes \\. Try this.
var new_html = "foo and bar(arg)";
var bad_string = "bar\\(arg\\)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
Demo
Your RegEx definition string should be:
var bad_string = "bar\\(arg\\)";
Special characters need to be escaped when using RegEx, and because you are building the RegEx in a string you need to escape your escape character :P
http://www.regular-expressions.info/characters.html
You need to escape the special characters contained in string you are creating your Regex from. For example, define this function:
function escapeRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}
And use it to assign the result to your bad_string variable:
let bad_string = "bar(arg)"
bad_string = escapeRegex(bad_string)
// You can now use the string to create the Regex :v: