invalid Regex group [duplicate] - javascript

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.

Related

Remove single quotes and last comma in jquery string using Regex [duplicate]

This question already has answers here:
JavaScript REGEX Match all and replace
(2 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a data
'abc','def','ghi',
I want to remove the single quote on all character and want to remove only the last comma, example like below
abc,def,ghi
How would i achieve that using regex for javascript?
I tried using this regex
.replace(^\'|,\s*$,"");
But seems like it is only removing the first quote as shown below
abc','def','ghi',
I am not very good in regex, i appreciate any help that i can get. Thanks
try this:
.replace(/\'|,$/g, "");
the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match
There is an easy way, use the $ operator
.replace(/'|,$/g, '')
Can you please check replace(/'|(,)$/g," ") and it should work.

Javascript Regex multi space "\s" doesn't work in chrome [duplicate]

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

Convert string into Regular Expression in Javascript [duplicate]

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.

JavaScript special characters misunderstanding [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
In this javascript code I have this row:
var regex = /\s+/gi;
Any idea what is the maning of this:
/\s+/gi
Thank you in advance.
Here is a must read for same https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods of RegExp, and with the match, replace, search, and split methods of String. This chapter describes JavaScript regular expressions.
Here, each contiguous string of space characters is being replaced with the empty string
For more details refer :-
Is there a difference between /\s/g and /\s+/g?

How to fix my regex? [duplicate]

This question already has answers here:
Matching a Forward Slash with a regex
(9 answers)
Closed 8 years ago.
I tried making a regex expression to search for in the string named 'patt'. Unfortunately the following gives an error in dreamweaver:
patt.search(/.*://.*waw\d.omegle.com/);
I need to get this pattern working. What am i doing wrong here?
You need to escape the / in the pattern as
patt.search(/.*:\/\/.*waw\d\.omegle\.com/);
Also escape . for more saftey as . alone could match anything in regex
Example
var patt = "http://asdfwaw1.omegle.com";
patt.search(/.*\/\/.*waw\d\.omegle\.com/);
=> True
because it thinks the / in // is the end of the reg exp. If you look at the coloring in your code above you can see the brown color ends at the first slash. You need to escape it.
/.*:\/\/.*waw\d.omegle.com/;

Categories

Resources