How can I add variable inside RegEx [duplicate] - javascript

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 4 years ago.
I am not finding solution to add variable inside following RegEx
val.replace(/[^\d.]/gm,'')
Basically I need to change the . to variable.
Something like
val.replace("/[^\d"+sepaeator+"]"/gm,'')

Use a RegExp instance (mdn):
const regex = new RegExp(`[^\d${separator}]`, 'gm');
val.replace(regex, '');

Related

Regex how to get what comes after a certain word in a string? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903
I am trying to get whatever comes after 'code=' from this string. How could I do this?
You could use
code=(.+)
See a demo on regex101.com.
In JavaScript this could be:
let string = 'http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903';
let m = string.match(/code=(.+)/);
console.log(m[1]);
But it would possibly be more straight-forward to parse the url as it is and use the query part accordingly.
str.match(/code=(.*)/)[1] will do that for you.

Replace all in javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
How do I replace all occurences of "http://localhost" in a string in javascript?
I have res=res.replace(/^http\:\/\/localhost, url);, but it does not work. How do I fix it?
url is a variable and but "localhost" is a string.
UPDATE:
With the solutions below, I still get: ReferenceError: localhost is not defined. What am I missing?
UPDATE 2:
This is the (Perl) code that inserts the JS on the page:
$form .= qq|<script>res='$doc'; loc=document.location.href; url=loc.substring(0,loc.indexOf(":8080")); res=res.replace(/http\:\/\/localhost/g, url); document.location='data:text/html;charset:utf-8,' + res; </script>|;
use /g to replace every occurrence in your string
Something like this
str = str.replace(/http:\/\/localhost/g,'');
res=res.replace(/http\:\/\/localhost/g, url);
'g' indicates to replace all.

Using variable in building a Regular Expression [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 7 years ago.
I have seen several similar questions on SO, but not exactly what I'm looking for.
I want to use a variable in my regEx so that when I call it, I can easily pass in a number.
Here's the hard coded regEx:
'mywonderfullString'.match(/.{1,3}/g)
Here's what I need:
'mywonderfullString'.match(/.{1,variableHERE}/g)
So when I call the regEx, I would do something like
'mywonderfullString'.match(/.{1,3}/g)
I've seen some examples using the replace regEx, but I can't seem to my example working.
You need to use RegExp constructor in-order to include variables inside regex.
var variableHERE = '3'
alert('mywonderfullString'.match(new RegExp(".{1," + variableHERE + "}", "g")))

Ignoring line breaks in Javascript regex [duplicate]

This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 4 years ago.
Even if I use the m flag, javascript regex seems to isolate regex matching by lines.
Example:
"if\nend".match(/if(.*?)end/m)
=> null
I want this to match. How do I get around this?
You actually want s (a.k.a. "dotall"), not m, but javascript doesn't support that. A workaround:
"if\nend".match(/if([\s\S]*?)end/)

new Regexp doesn't work [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 convert following expression to "new Regexp()" style:
http://jsfiddle.net/HDWBZ/
var Wyrazenie = /\btest[a-z]*/g;
I'm really confused with it and have no idea how to fix it. Below is what I've done but obviously it doesn't work.
var Wyraznie = new RegExp("\btest[a-z]*","g");
Also have a question how would it look if instead of "test" I would use variable?
You should use this instead...
new RegExp("\\btest[a-z]*", "g");
... as \b will be interpolated into a single (slashless) character when JavaScript parser works through the corresponding string literal. The solution is to escape slash itself.
DEMO: http://jsfiddle.net/HDWBZ/1/

Categories

Resources