Need help to use regex in match function of javascript [duplicate] - javascript

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 5 years ago.
I am using a regex for getting some pattern data and it is working fine but I need to use some part of regex from a variable but I am unable to do that please help me if somebody know how to make it.
var subStr = document_root.documentElement.outerHTML.match(/http(.*?Frover.fff.com.*?)\;/g);
alert(subStr[0]);
I get first result in alert but need to do it like this.
var link=rover.fff.com;
var regexpattern="/http(.*?F"+link+".*?)\;/g"
var rex=new RegExp(regexpattern);
var subStr = document_root.documentElement.outerHTML.match(regexpattern);
alert(subStr[0]);
I don't get anything.
Please help me.
Comment if you confuse in anything.

In JavaScript, when you need to use a variable, you have to create a new RegExp object, as you've tried.
It takes two parameters. The first is a string of the pattern (without the slashes at the start and end). The second is any flags (like your g).
So, your pattern would be something like this:
var regexpattern = new RegExp('http(.*?F' + link + '.*?)\\;', 'g')
Note, that you have to escape any special characters in the string like you would any other string (in this case, your \ had to be escaped to \\ so it'll work properly).
After that, you use it the same as the /pattern/ form. In your case, be sure to use rex, not regexpattern in your match() function.
(If so some reason that wasn't a literal \ and you were escaping the semi-colon, just remove it completely, you won't need to escape a semi-colon.)

Related

Replace quote sign in JavaScipt [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I got following code snippet, it's pretty simple,
var b = "'aa','bb'";
console.log(b.replace("'", ""));
// result is "aa','bb'"
I want replace all single quote signs with blank. So my expected output should be "aa,bb", but the actual output is "aa','bb'" neither run this code snippet in Node nor browser. Seems only the first single quote sign has been replaced.
I already got a workaround to resolve this problem by replace with regex. What I wanna know
is what happened to replace function here? I cannot figure this out.
Try using RegEx specifying the global flag (g) that matches the pattern multiple times. Please also note that, as replace() does not modify the original string you have to reassign the modified value to the variable:
var b = "'aa','bb'";
b = b.replace(/'/g, "");
console.log(b);

Javascript regex that escapes \ sign [duplicate]

This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)

Need regex to replace entire folderpath while preserving file name where last folder could by anything [duplicate]

This question already has answers here:
How to get the file name from a full path using JavaScript?
(21 answers)
Closed 6 years ago.
I need a regex that would replace something like this but leave the file name.
/folder1/folder2/folder3/anything/somefile.html
Also could someone show me how to implement this with replace method? Replacing the entire path match to empty string and again leaving the file and which would be anything.
Thanks in advance.
You can do it without regular expressions:
var filename = string.split('/').pop();
// "somefile.html"
You can use .*\/.
. will match anything
* will repeat the previous zero or more times.
\/ is a literal slash (/). But needs to be escaped because it's part of the regex construct:
var str = '/folder1/folder2/folder3/anything/somefile.html';
str.replace(/.*\//, ''); // "somefile.html"

Is using var foo = new RegExp necessary? [duplicate]

This question already has answers here:
Are /regex/ Literals always RegExp Objects?
(3 answers)
Closed 10 years ago.
While researching the use of regular expressions in javascript, one can encounter examples of two types:
A:
var regex = /^[a-zA-Z0-9]+$/;
B:
var regex = new RegExp ("^[a-zA-Z0-9]*$");
Is it necessary to use var foo = new RegExp? Or, when should one pick each method?
The RegExp() constructor is useful when you have to assemble a regular expression dynamically at run time. If the expression is completely static, it's easier to use the native regex syntax (your "A"). The ease-of-use of the native syntax stems from the fact that you don't have to worry about quoting backslashes, as you do when your regular expression begins life as a string constant.
Is it necessary to use var foo = new RegExp?
No, obviously not. The other one works as well.
Or, when should one pick each method?
Regex literals are easier to read and write as you do not need to string-escape regex escape characters - you can just use them (backslashes, quotes). Also, they are parsed only once during script "compilation" - nothing needs to be executed each time you the line is evaluated.
The RegExp constructor only needs to be used if you want to build regexes dynamically.
Here's an example of a 'dynamic' regular expression where you might need new RegExp.
var search = 'dog',
re = new RegExp('.*' + search + '.*');
If it's a static regular expression, then the literal syntax (your A option) is better because it's easier to write and read.

JavaScript .replace doesn't replace all occurrences [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript multiple replace
How do I replace all occurrences of "/" in a string with "_" in JavaScript?
In JavaScript, "11.111.11".replace(".", "") results in "11111.11". How can that be?
Firebug Screenshot:
Quote from the doc:
To perform a global search and replace, either include the g switch in
the regular expression or if the first parameter is a string, include
g in the flags parameter. Note:
The flags argument does not work in v8 Core (Chrome and Node.js) and will be removed from Firefox.
So it should be:
"11.111.11".replace(/\./g, '');
This version (at the moment of edit) does work in Firefox...
"11.111.11".replace('.', '', 'g');
... but, as noted at the very MDN page, its support will be dropped soon.
With a regular expression and flag g you got the expected result
"11.111.11".replace(/\./g, "")
it's IMPORTANT to use a regular expression because this:
"11.111.11".replace('.', '', 'g'); // dont' use it!!
is not standard
First of all, replace() is a javascript function, and not a jquery function.
The above code replaces only the first occurrence of "." (not every occurrence). To replace every occurrence of a string in JavaScript, you must provide the replace() method a regular expression with a global modifier as the first parameter, like this:
"11.111.11".replace(/\./g,'')

Categories

Resources