Variable in regular expression [duplicate] - javascript

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"]]

Related

how to find the first number from string in js(javascript)? [duplicate]

This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
Closed 6 months ago.
How to find the first number from string in javascript?
var string = "120-250";
var string = "120,250";
var string = "120 | 250";
Here is an example that may help you understand.
Use the search() method to get the index of the first number in the string.
The search method takes a regular expression and returns the index of the first match in the string.
const str = 'one 2 three 4'
const index = str.search(/[0-9]/);
console.log(index); // 4
const firstNum = Number(str[index]);
console.log(firstNum); // 2
Basic regular expression start of string followed by numbers /^\d+/
const getStart = str => str.match(/^\d+/)?.[0];
console.log(getStart("123,456"));
console.log(getStart("123-456"));
console.log(getStart("123|456"));
console.log(getStart("xxx,xxx"));
Or parseInt can be used, but it will drop leading zeros.
const getStart = str => parseInt(str, 10);
console.log(getStart("123,456"));
console.log(getStart("123-456"));
console.log(getStart("123|456"));
console.log(getStart("xxx,xxx"));

New Regex from literate string [duplicate]

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

Get specific charters from raw string [duplicate]

This question already has answers here:
Javascript - return string between square brackets
(6 answers)
regex to get all text outside of brackets
(4 answers)
regex to get String which is outside of brackets
(1 answer)
Regex to extract the string both inside and outside a square bracket
(2 answers)
Closed 3 years ago.
I have a raw string like "fsfsdfsdd [qwewqewe] sdfsdfdf sdfsd[sdf]". How to get value from inside of [] this box and outside of [] this box. O/P fsfsdfsdd,sdfsdfdf,sdfsd & qwewqewe,sdf If it is possible to using JavaScript RegExp?
Try this solution:
str = "fsfsdfsdd [qwewqewe] sdfsdfdf sdfsd[sdf]";
s = str.match(/\[(\w+?)\]/g);
s.forEach(i => console.log(i));
Without square brackets:
res = Array.from(str.matchAll(/\[(.+?)\]/g));
res.forEach(i => console.log(i[1]));
let str = "fsfsdfsdd [qwewqewe] sdfsdfdf sdfsd[sdf]";
let regex = /(?<=\[)(.*?)(?=\])/g;
let s1 = str.match(regex); // strings in the brackets
let s2 = str.replace(regex, '').replace(/[[]]/g, '').replace(/\s/, '').split(' '); // strings outside the brackets
let result = [...s1, ...s2]
console.log(result)

Regex replace with captured [duplicate]

This question already has answers here:
Using $0 to refer to entire match in Javascript's String.replace
(2 answers)
Closed 5 years ago.
I want replace all non alphanumeric character in the string by it self surrender for "[" and "]".
I tried this:
var text = "ab!#1b*. ef";
var regex = /\W/g;
var result = text.replace(regex, "[$0]");
console.log(result);
I was expecting to get:
ab[!][#]1b[*][.][ ]ef
But instead i get:
ab[$0][$0]1b[$0][$0][$0]ef
How can I do this using Javascript(node)?
You need to wrap the group in parentheses to assign it to $1, like this:
var text = "ab!#1b*. ef";
var regex = /(\W)/g;
var result = text.replace(regex, "[$1]");
console.log(result);

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

Categories

Resources