This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
This javascript regex needs to extract "131PS" from "this is (131PS) for now".
Then I need to get "131" as a number and "PS" as a string.
Any suggestions? thx
myString.match(/\(([^\)]+)\)/ig)[0]
returns (131PS) which is not what was expected.
You need to work with capture regex groups () to withdraw the number and string separately, have a look:
let rawStr = "this is (131PS) for now";
let theMatch = rawStr.match(/\((\d+)([A-Z]+)\)/);
if (theMatch) {
let theNum = parseInt(theMatch[1]);
let theString = theMatch[2];
console.log(theNum, theString);
}
Related
This question already has answers here:
Case insensitive replace all
(7 answers)
Closed 2 years ago.
My code is as follows:
array.forEach(el => {
string = string.replace(el, `censored`);
});
array : my array of words that I want to censor.
string : the string that the words need censoring.
My issue is that this process is quite slow and also if the word in my string is written using capitals, it's getting missed.
Any ideas how should I solve this issue?
Thank you.
maybe you can use regex
let array = ['mate']
let string = 'Hello Mate, how are you mate?'
let re = new RegExp(array.join("|"),"gi");
let str = string.replace(re, 'censored');
output:
"Hello censored, how are you censored?"
This question already has answers here:
Why does javascript replace only first instance when using replace? [duplicate]
(3 answers)
Closed 3 years ago.
I need to remove 2 words from a string. The words are _with and _and so raised_hand_with_fingers_and_splayed becomes raised_hand_fingers_splayed
The regex /_with|_and/ appears to work in https://regexr.com/ but when I use it with JavaScript only the _with is removed:
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/,"")
You need the g modifier to perform multiple replacements. Otherwise it just replaces the first match.
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/g,"")
console.log(newStr);
This question already has answers here:
Regex to replace everything except numbers and a decimal point
(7 answers)
Closed 3 years ago.
I am trying to write a regex that replaces anything that isn't a digit or a . in a string.
For example:
const string = 'I am a 1a.23.s12h31 dog'`
const result = string.replace(/[09.-]/g, '');
// result should be `1.23.1231`
Can anyone see what I am doing wrong here.
You could change your regex to [^0-9.]+:
const result = string.replace(/[^0-9.]+/g, "");
Alternatively, if you don't want a regex, use split and filter, then join:
const result = string.split("").filter(s => isNaN(s) || s == ".").join("");
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
I have the following URL
https://website.com?id=XXXVVVCCCHHH
I only want XXXVVVCCCHHH, I've tried the following:
var phrase = 'https://website.com?id=XXXVVVCCCHHH';
var myRegexp = /id=(.*)/;
phrase = myRegexp.exec(phrase);
But this is returning: id=XXXVVVCCCHHH;
How can Ii edit this to only return XXXVVVCCCHHH?
Just use split and take the second element:
var url = "https://website.com?id=XXXVVVCCCHHH";
var part = url.split('=')[1];
console.log(part);
This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');