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

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

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.

invalid Regex group [duplicate]

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.

Split string on newline and comma [duplicate]

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 7 years ago.
My input String is like
abc,def,wer,str
Currently its splitting only on comma but in future it will contain both comma and newline.
Current code as below:
$scope.memArray = $scope.memberList.split(",");
In future I need to split on both comma and newline what should be the regex to split both on comma and newline.
I tried - /,\n\ but its not working.
You can use a regex:
var splitted = "a\nb,c,d,e\nf".split(/[\n,]/);
document.write(JSON.stringify(splitted));
Explanation: [...] defines a "character class", which means any character from those in the brackets.
p.s. splitted is grammatically incorrect. Who cares if it's descriptive though?
You could replace all the newlines with a comma before splitting.
$scope.memberList.replace(/\n/g, ",").split(",")
Try
.split(/[\n,]+/)
this regex should work.

same dynamic regex and inline regex not giving same output in javascript [duplicate]

This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 7 years ago.
I have been staring at these two flavors of same regex and can't figure out why the outcome is different:
var projectName="SAMPLE_PROJECT",
fileName="1234_SAMPLE_PROJECT",
re1 = new RegExp('^(\d+)_SAMPLE_PROJECT$','gi'),
re2 = /^(\d+)_SAMPLE_PROJECT$/gi,
matches1 = re1.exec(fileName),
matches2 = re2.exec(fileName);
console.log(matches1);//returns null
console.log(matches2);//returns correctly
Here is the jsbin : https://jsbin.com/badoqokumu/edit?html,js,output
Any idea what I must be doing wrong with instantiating RegExp?
Thanks.
In the first case, you have a string literal, which uses \ to introduce escape sequences. \d in a string is just d. If you want \d, you need to type \\d instead.
In the second case, you have a regular expression literal, which does not interpret \ as a string escape sequence.

Escape a '+' plus sign in a string to be used in a regex in coffeescript/javascript [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 4 years ago.
I have a regex I'm running to filter rows in a table. The filtering is done in Javascript.
I'm writing coffeescript, but a Javascript solution would be fine -- I can just translate it to coffeescript myself.
I have a value role that contains a string I want to filter on using a regex. The problem is the string role may or may not have embedded '+' signs in it. Plus signs are special characters for regex searches and need to be escaped in the search string.
I create the regex search string like this (coffeescript):
"^"+role+"$"
How can I preprocess role to escape any '+' signs so the regex works?
+ is far from the only character with special meaning. Here is a function that will escape all the necessary characters:
function regex_escape(str) {
return str.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&');
}

Categories

Resources