This question already has answers here:
How to escape regular expression special characters using javascript? [duplicate]
(3 answers)
Closed 2 years ago.
Sorry, this sounds very basic, but I really can't find on Google.
In order to replace contents in a string globally, you can use things like...
a.replace(/blue/g,'red')
But sometimes you need to replace characters that's not compatible with the example above, for example, the character ")"
So, this will fail...
const a ="Test(123)"
a = a.replace(/(/g, '')
VM326:1 Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
How to replace string of characters like that ?
at :1:7
The special regular expression characters are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
const a ="Test(123)";
console.log(a.replace(/\(/g, ''));
you need to use the escape char \ for this. there are set of char which need to be escaped in this replace(regex)
a.replace(/\(/g, '');
Find a full details here at MDN
you need to escape the ( with \ in a new variable because a is const
and it will work
var b = a.replace(/\(/g, '');
for more practicing use this site
regExr
Related
This question already has answers here:
Negate characters in Regular Expression [closed]
(4 answers)
Closed 9 months ago.
I need to prevent user from entering the following set of chars:
~ " # % & * : < > ? / \ { | } .
Important is that, anything else will be allowed, but only the characters shown above will be forbidden.
Currently, I'm using the following regex but this does also forbid for example ! sign.
private static folderRegex = /^[A-Za-z0-9_-]+[ßüÜöÖäÄ\w]*$/;
Just put the set of characters in an inverse set and add a ^ and a $ to stretch the pattern to the full length of the input.
private static folderRegex = /^[^~"#%&*:<>?\/\\{|}]+$/;
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How to properly escape characters in regexp
(4 answers)
Closed 4 years ago.
I can't understand when I need one and two backward slashes \.
First, see an example:
const rxHttps = new RegExp("\/");
console.log(rxHttps.test("/")); // true
const rxQuestion = new RegExp("\\?");
console.log(rxQuestion.test("?")); // true
const rxAnotherQuestion = new RegExp("\?"); // Uncaught SyntaxError: Invalid regular expression: /?/: Nothing to repeat
In the above example, to use a character /, it needs just one \.
However, ? needs two \, or SyntaxError occurs.
This is really confusing. I can't make heads or tails of it.
Why are they different? Am I misunderstanding?
because '?' is used to mean that what's before it can or not exist like {0,}
for example
var reg = new RegExp('a?');
// this has the same meaning as using
// var reg = new RegExp('a\?');
reg.test('a'); // will display true
reg.test('anything'); // this will also display true
reg.test('123'); // this also displays true
so if you want to check of the existence of '?' character, you have to escape it, and since it's used as '\?' also, then you have to escape it using '\?'
Why does it give you syntax error ?
because it's expecting something to check if it exists or not and you're not giving it anything.
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I'm trying to further my understanding of regular expressions in JavaScript.
So I have a form that allows a user to provide any string of characters. I'd like to take that string and remove any character that isn't a number, parenthesis, +, -, *, /, or ^. I'm trying to write a negating regex to grab anything that isn't valid and remove it. So far the code concerning this issue looks like this:
var pattern = /[^-\+\(\)\*\/\^0-9]*/g;
function validate (form) {
var string = form.input.value;
string.replace(pattern, '');
alert(string);
};
This regex works as intended on http://www.infobyip.com/regularexpressioncalculator.php regex tester, but always alerts with the exact string I supply without making any changes in the calculator. Any advice or pointers would be greatly appreciated.
The replace method doesn't modify the string. It creates a new string with the result of the replacement and returns it. You need to assign the result of the replacement back to the variable:
string = string.replace(pattern, '');
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 4 years ago.
I'm trying to create a dynamic regex to select URL's based on a segment or the whole URL.
For example, I need to get var.match(/http:\/\/www.something.com\/something/)
The text inside the match() needs to be converted so that special characters have \ in front of them such for example "\/". I was not able to find a function that converts the URL to do this? Is there one?
If not, what characters require a \ in front?
I use this to escape a string when generating a dynamic regex:
var specials = /[*.+?|^$()\[\]{}\\]/g;
var url_re = RegExp(url.replace(specials, "\\$&"));
( ) [ ] ? * ^ $ \ . + | and in your case, / since you're using that as the delimiter in the match.
Further info mostly to pre-empt comments and downvotes: I don't really know where - come from as a special character. It's only special when inside character class brackets [ and ] which you're already escaping. If you want to include characters that are sometimes special (which the OP doesn't) that would include look-ahead/behind characters as well, which include =, < and >.
This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
qAnswersR[90430] = [];
qAnswersR[90430].push("[math]k: \frac{(x+20)^{2}}{256}+\frac{(y-15)^{2}}{81}=1[/math]");
And I need to get the value into variable, but when I console.log out the array like this:
console.log(qAnswersR[90430]);
I get: [math]k: rac{(x+20)^{2}}{256}+rac{(y-15)^{2}}{81}=1[/math],[math]k: 81(x+20)^{2}+256(y-15)^{2}=20736[/math]
But the escape tag "\" disappears, but I need it there, what should I do?
But the escape tag "\" disappears, but I need it there, what should I do?
You need to escape the backslash, i.e., use \\ instead of just \:
"[math]k: \\frac{(x+20)^{2}}{256}+\\frac{(y-15)^{2}}{81}=1[/math]"
^ ^
You can use tagged template literals
var str = (s => s.raw)`[math]k: \frac{(x+20)^{2}}{256}+\frac{(y-15)^{2}}{81}=1[/math]`[0]
The anonymous arrow function will serve as tag and s.raw contains the original input
Escape the escape character, like \\a.