This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
JavaScript regex: Positive lookbehind alternative (for Safari and other browsers that do not support lookbehinds)
(1 answer)
Closed 23 days ago.
Test this regex on regex101.com. It works just fine.
The regular expression is:
(?<=\/blog\/author\/)[^/]+\/?(\d+)?\/?$
And the test string is:
/blog/author/john/2/
But when I use new RegExp it fails
Why is it inconsistent? Is regex101.com an invalid reference for JS regular expressions?
const str = '/blog/author/john/2/';
const re = new RegExp('(?<=\/blog\/author\/)[^/]+\/?(\d+)?\/?$','gm');
console.log(re)
console.log(re.test(str))
Related
This question already has answers here:
JavaScript regular expression "Nothing to repeat" error
(2 answers)
Why do regex constructors need to be double escaped?
(5 answers)
Closed 3 years ago.
I trying to identify regular expression in JS. Expression which wrote to identify the pattern is working in Online RegEx but when i use it in my own Code it does not work.
it says error .*|{1}/: Nothing to repeat
This is Expression -
Examples:\n.*\|{1}
Sample Data -
Examples:
|asda| asd|asd|adasda|adaasdsaasdasdsa|adsadsa|asdasdad|asdasdsd|asda |
Examples:
|Word| asd|asd|W#132|iam|GOto|asdasdad|asdasdsd|asda- |
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 6 years ago.
I try to use this Regex
new RegExp('utm_source=Weekly\SRecommended\SJobs\SEmail', 'ig');
When I try to use it in regex101 or regexr it works.
And in my code doesn't work.
I try to use this in console this is the result is
/utm_source=WeeklysRecommendedsJobssEmail/gi
the code without spaces.
When I try to use space letter it works.
Any help?
Because \ is an escape character for regular expressions and strings. You have to escape the \ if you're creating a regex from a string:
new RegExp('utm_source=Weekly\\SRecommended\\SJobs\\SEmail', 'ig');
Or simply use a regex literal which exists precisely to avoid this problem:
/utm_source=Weekly\SRecommended\SJobs\SEmail/ig
This question already has an answer here:
Regex expression throwing error in IE 11 [duplicate]
(1 answer)
Closed 6 years ago.
I have created the following RegExp object:
RegExp(/_if|_elseif|_else|_while|_store/, "g")
I need to match either of the pipe delim strings. The above method works in chrome. But the IE11 throws error saying some syntax error.
That's because the RegExp constructor in IE11 only takes a string as its first argument, and not a regular expression literal:
RegExp("_if|_elseif|_else|_while|_store", "g")
^ ^
Alternatively, you can simply add the g flag to the end of your regular expression literal and drop the constructor notation altogether:
/_if|_elseif|_else|_while|_store/g
This question already has answers here:
Building regexp from JS variables not working
(5 answers)
Closed 7 years ago.
From the backend of my application, I receive a regular expression which should be matched with a postal code in the frontend.
However, every time I convert to string into a regular expression using the RegExp class, I get another regular expression which doesn't match my postal code anymore.
This is the code I'm currently using (copy from my console):
var str = '/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/',
exp = new RegExp(str);
// Returns null
'1055AA'.match(exp);
// The code below does work though...
// Returns: ["1055AA", "AA"]
'1055AA'.match(/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/);
Can someone help me solve this problem? Thanks!
Your input string must not begin and end with the Regexp markers / - after all, it's a regular string, not a literal regexp. Also, since it's a regular string (and not (yet) a regexp), you need to double the backslashes as usual in a regular string.