New Regex from literate string [duplicate] - javascript

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 = '\\)';

Related

Variable in regular expression [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
How can I replace the "#" with the constant “begins” in the regular expression
function matchAlltext() {
const str = document.getElementById('textinput').value;
const begins = document.getElementById('begins').value;
const regexp = new RegExp(/#\w+/ig);
let matchAll = str.matchAll(regexp);
matchAll = Array.from(matchAll);
document.getElementById('textoutput').value = matchAll;
}
I need to find all the words in the text starting with the character entered in the input field
Like this?
const str = "abc abc abc"
const begins = "a"
const regexp = new RegExp(begins + "\\w+","ig");
let matchAll = str.matchAll(regexp);
matchAll = Array.from(matchAll);
console.log(matchAll); //logs [["abc"], ["abc"], ["abc"]]

String.split with regexp bug [duplicate]

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());

javascript regex doesn't work with RegExp [duplicate]

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

How to delete all words with "-" before in Javascript [duplicate]

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(' '));

How can I parse a string and split it into two parts at a special character with javascript? [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
How can I take a string and split it at a special character into two new variables (and remove the special chars) with javascript?
For example take:
var X = Peggy Sue - Teacher
and turn it into:
varnew1 = Peggy Sue
varnew2 = Teacher
I guess it should also include a condition... if the string has a "-" then do this.
.split is probably what you want. Here is a very simple example
JSFiddle Link
var string = 'Peggy Sue - Teacher'
var new1 = string.split('-')[0].trim();
var new2 = string.split('-')[1].trim();
console.log(new1); // "Peggy Sue"
console.log(new2); // "Teacher"
And if you want to place a simple condition on it looking for - you can do so with the following
var string = 'Peggy Sue - Teacher'
var new1 = string.indexOf('-') !== -1 ? string.split('-')[0].trim() : string
var new2 = string.indexOf('-') !== -1 ? string.split('-')[1].trim() : string
Second Fiddle
var result = str.split("-");
will give you an array with 2 members,
result[0] = Peggy Sue
result[1] = Teacher

Categories

Resources