This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 5 years ago.
Try to use node read a file and use regex to do something
const langFiles = 'test.php';
const fs = require('fs');
const data = fs.readFileSync(langFiles, 'utf8');
var pat = "\".*\.\w+\"" ;
var rex = new RegExp( pat, "gim" ) ;
var rep = "$1" ;
var res = data.replace( rex, rep ) ;
console.log( res ) ;
My regex already correct but my console result show the entire file, it seems it didn't execute the regex? test.php look like this https://pastebin.com/YFUD6AiE
Related
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 2 years ago.
I don't understand why rg = new RegExp(`/${a}.*${b}/`) doesn't work below whereas rg = /\(.*\)/ does work.
let a = '\(';
let b = '\)';
let rg;
//rg = new RegExp(`/${a}.*${b}/`); // doesn't work
rg = /\(.*\)/;
let match = rg.exec(`test (regex works) is ok`);
if (match) {
console.log(match[0]); // -> (regex works)
}
Because you would need to double escape the \ in the string.
let a = '\\(';
let b = '\\)';
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regular expression to stop at first match
(9 answers)
Closed 3 years ago.
I have a content like this.
<strong>Citation1:</strong> Firs<b style="font-weight:bold">t</b> Ci<b style="font-weight:bold">t</b>a<b style="font-weight:bold">t</b>ion
I want to remove all <b> tag using regex I am using below but its not working with multiple b tag.
function removeBoldString(str) {
const re = new RegExp('(<b(.*)">)|(</b>)', 'g');
return str.replace(re, '');
}
You'll need to use something like [^>]* instead of .*, here is an example:
const str = `<strong>Citation1:</strong> Firs<b style="font-weight:bold">t</b> Ci<b style="font-weight:bold">t</b>a<b style="font-weight:bold">t</b>ion`;
function removeBoldString(str) {
const re = new RegExp('(<b([^>]*)">)|(</b>)', 'g');
return str.replace(re, '');
}
const result = removeBoldString(str);
console.log(result);
But it isn't a good idea to process HTML using regex, there are a lot of methods of processing HTML in JavaScript, especially if you're doing this in a browser. Here is an example:
const str = `<strong>Citation1:</strong> Firs<b style="font-weight:bold">t</b> Ci<b style="font-weight:bold">t</b>a<b style="font-weight:bold">t</b>ion`;
const doc = new DOMParser().parseFromString(str, 'text/html');
doc.querySelectorAll('b').forEach((b) => {
b.replaceWith(doc.createTextNode(b.textContent));
});
console.log(doc.body.innerHTML);
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I want to split a string by a variable number of successive characters
splitBy4('XXXXXXXX') => ['XXXX', 'XXXX']
Before injecting the variable it worked all fine :
console.log('XXXXXXXX'.split(/(\w{4})/).filter(Boolean));
// outputs : ['XXXX', 'XXXX']
console.log('XXXXXXXX'.split(new RegExp(/(\w{4})/)).filter(Boolean));
// outputs : ['XXXX', 'XXXX']
But when I try to use the RegExp class + string representation (to inject my parameter), it fails :
console.log('XXXXXXXX'.split(new RegExp('(\w{4})')).filter(Boolean));
// outputs ['XXXXXXXX']
const nb = 4;
console.log('XXXXXXXX'.split(new RegExp('(\w{'+ nb +'})')).filter(Boolean));
// outputs ['XXXXXXXX']
What am I missing and how can I inject my parameter ?
Thanks
const nb = "4";
var myRegex = new RegExp('(\\w{' + nb + '})', 'g');
var myArray = myRegex.exec('XXXXXXXX');
console.log(myArray.toString());
This question already has answers here:
Regex, replace all words starting with #
(5 answers)
Closed 5 years ago.
I'm totally new to regex and can't figure out how to realize it via Javascript.
For example, I have the string var string = "-just an example -string for stackoverflow". The expected result is string = "an example for stackoverflow".
Thanks in advance
Try this:
-\w+\s+
and replace by empty
Regex Demo
const regex = /-\w+\s+/gm;
const str = `-just an example -string for stackoverflow`;
const subst = ``;
const result = str.replace(regex, subst);
console.log(result);
Try this with simple forEach.
var string = "-just an example -string for stackoverflow";
var strArray = string.split(' ');
strArray.forEach(function(value, i){
if(value.startsWith('-')){
strArray.splice( i, 1 )
}
});
console.log(strArray.join(' '));
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
I have this code
$("#title").keyup(function(){
var titleval = $("#title").val();
var res = titleval.replace(" ", "-");
$("#newsurl").val(res);
});
to replace spaces into dash to get URL like this
wordone-wordtow-wordthree
but i have problem with this code it's just replace first space like this
wordone-wordtow wordthree
How can i solve this problem
You need to do a global match, you can do this with a regex
var res = titleval.replace(/\s/g, "-");
Though String.prototype.replace does support having flags passed, this is deprecated in firefox and already doesn't work in chrome/v8.
Alternate method (if regex is not mandatory) could be to split and join
var res = titleval.split(" ").join("-");
or
var res = titleval.split(/\s+/).join("-");
Use regex with global flag
titleval.replace(/\s/g, "-");
try like this:
$("#title").keyup(function(){
var titleval = $("#title").val();
var res = titleval.replace(/\s+/g, '-');
$("#newsurl").val(res);
});