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 >.
Related
This question already has an answer here:
javascript regexp replace not working, but string replace works
(1 answer)
Closed 1 year ago.
Hello team I am new to JS so I am trying to use RegEx with replacing to take input from the user and replace it if it doesn't match the RegEx I have to be able to put 7 digits or 6 digits followed with one letter currently I am doing this
someID.replace('^(([0-9]{1,7})|([0-9]{1,6}[a-zA-Z]{1}))$')
I am not able to replace the current string with the RegEx expression if I enter
12345678900 it remain the same in that situation I need to be 1234567 after the replace or if I have 12345678asd to be 123456a. How can I achieve that by only replace function and a RegEx expresion
You need to use a different regex and a dirrent replace function.
You will also need to get rid of $ if you want to be able to successfully match the string, without worrying about how it ends.
const sampleIDs = [
"123456789000",
"123456abc",
];
sampleIDs.forEach(id => {
const clean = id.match(/^\d{6}[\d\D]/);
console.log(clean[0]);
});
This question already has answers here:
Getting content between curly braces in JavaScript with regex
(5 answers)
Closed 2 years ago.
I need to match a specific regex syntax and split them so that we can match them to an equivalent value from a dictionary.
Input:
{Expr "string"}
{Expr "string"}{Expr}
Current code:
value.match(/\{.*\}$/g)
Desired Output:
[{Expr "string"}]
[{Expr "string"},{Expr}]
Use a non-greedy quantifier .*?. And don't use $, because that forces it to match all the way to the end of the string.
value = '{Expr "string"}{Expr}'
console.log(value.match(/\{.*?\}/g));
One option, assuming your version of JavaScript support it, would be to split the input on the following regex pattern:
(?<=\})(?=\{)
This says to split at each }{ junction between two terms.
var input = "{Expr \"string\"}{Expr}";
var parts = input.split(/(?<=\})(?=\{)/);
console.log(parts);
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
This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a
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, '');