This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 7 years ago.
regularExpression = '[\w]:\\.*';
function validate() {
debugger;
var regex = new RegExp(regularExpression);
var ctrl = document.getElementById('txtValue');
if (regex.test(ctrl.value)) {
return true;
} else {
return false;
}
}
Above is my code with which I am validating a directory path being entered. Here is the link for validator website which shows the perfect match for C:\ or D:\ or D:\Abcd\List.odt. But when I try to run the above JavaScript it fails.
When the Regular Expression string '[\w]:\\.*' is parsed by the JavaScript's RegEx engine, it will treat \w as an escaped character and since \w has no special meaning as an escaped character, it will treated as w only.
To fix this, you need to escape the \ like this
var regularExpression = '[\\w]:\\.*';
Or use RegEx literal like this
var regularExpression = /[\w]:\.*/;
As pointed out by Cyrbil in the comments, the character class [\w] is the same as \w only. So you can safely omit the square brackets around \w.
Related
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 1 year ago.
I need to define below regex in Javascript:
(\bin region\b)(?!.*\1).+?(?=<)
I tried like below but it looks like not working:
var reg = new RegExp ('(\bin region\b)(?!.*\1).+?(?=<)');
Although the Atom tool matches the regex in the target string, JavaScript code is returning blank.
I am using this in Azure logic app (inline code executor connector)
Anyone can help me with this?
You can see it matches the text in Atom:
Inside a string you need to escape the backslashes.
Otherwise the backslash will escape the next character. So, writing \b will escape the character b instead use \\b which will escape the \
var reg = new RegExp ('(\\bin region\\b)(?!.*\\1).+?(?=<)');
This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 3 years ago.
I'm trying to replace forward slash with backward with below code but the result is not as expected, What's the expected second parameter for replace in this case?
var path = 'C:\Users\abc\AppData\Local\Programs\Python\Python37\python.exe';
path.replace(/\\/g, "/");
console.log(path)
result:
"C:UsersabcAppDataLocalProgramsPythonPython37python.exe"
Your regex is fine but variable declaration needs double backslash because single backslash is interpreted as escape character:
var path = 'C:\\Users\\abc\\AppData\\Local\\Programs\\Python\\Python37\\python.exe';
path = path.replace(/\\/g, "/");
console.log(path);
//=> C:/Users/abc/AppData/Local/Programs/Python/Python37/python.exe
If you want to avoid using \\ in assignment then you can use String.raw
var path = String.raw`C:\Users\abc\AppData\Local\Programs\Python\Python37\python.exe`;
path.replace(/\134/g,"/");
As a regular expression. \134 is the octal representation of a backslash
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Trying to replace a string that contains special characters. The purpose of this is to convert the query string into an understandable format for end users.
full string is:
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
Specifically the portion after ^NQ, in this example: opened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday(). I have split the original string with indexOf(^NQ) and passing the resulting sub-strings to a function. I'm then trying a .replace() as below:
var today = replacementString.replace(/(ONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday())/g, ' is today ');
replacementString = today;
I have tried with various combinations of the above line, but have not returned what I am hoping for.
I've had no issues replacing special characters, or strings without special characters, but the combination of the 2 is confusing/frustrating me.
Any suggestions or guidance would be appreciated
You should escape the () to \(\) to match it literally or else it would mean a capturing group. For the match you could also omit the outer parenthesis and you have to escape the dot \. to match it literally.
ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
var today = str.replace(/ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)/g, ' is today ');
replacementString = today;
console.log(today);
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Extra backslash needed in PHP regexp pattern
(4 answers)
Regex to replace single backslashes, excluding those followed by certain chars
(3 answers)
Closed 7 years ago.
function trim(str) {
var trimer = new RegExp("(^[\\s\\t\\xa0\\u3000]+)|([\\u3000\\xa0\\s\\t]+\x24)", "g");
return String(str).replace(trimer, "");
}
why have two '\' before 's' and 't'?
and what's this "[\s\t\xa0\u3000]" mean?
You're using a literal string.
In a literal string, the \ character is used to escape some other chars, for example \n (a new line) or \" (a double quote), and it must be escaped itself as \\. So when you want your string to have \s, you must write \\s in your string literal.
Thankfully JavaScript provides a better solution, Regular expression literals:
var trimer = /(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+\x24)/g
why have two '\' before 's' and 't'?
In regex the \ is an escape which tells regex that a special character follows. Because you are using it in a string literal you need to escape the \ with \.
and what's this "[\s\t\xa0\u3000]" mean?
It means to match one of the following characters:
\s white space.
\t tab character.
\xa0 non breaking space.
\u3000 wide space.
This function is inefficient because each time it is called it is converting a string to a regex and then it is compiling that regex. It would be more efficient to use a Regex literal not a string and compile the regex outside the function like the following:
var trimRegex = /(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g;
function trim(str) {
return String(str).replace(trimRegex, "");
}
Further to this \s will match any whitespace which includes tabs, the wide space and the non breaking space so you could simplify the regex to the following:
var trimRegex = /(^\s+)|(\s+$)/g;
Browsers now implement a trim function so you can use this and use a polyfill for older browsers. See this Answer
This question already has answers here:
Regular expression, How to allow combination of dot (period) and letters?
(7 answers)
Closed 3 years ago.
from regexp=^(?:[a-zA-Z]+(?:[.'\-,])?\s?)+$, how am I suppose to do it allowing only alphanumeric and dots? Thanks!
Try this:
regexp = ^[a-zA-Z0-9\.]+$
This will get alphnum and dots..
/[a-zA-Z0-9.]/
In javascript the regular expressions shouldn't be in quotes, but in slashes. For exapmple:
var myregexp = /[a-z0-9\.]+/i;
var myvar = "I am a regular expression.";
var result = myregexp.test(myvar);
returns true, because in myvar there are only digits, letters and dots.
Note: the dots are special symbols and must be preceded by \