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');
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
I want to remove all "0" in my string, not only the first same value, any suggest?
Why its work but just only the first code
var str = "90807005"
console.log(str.replace("0",""))
I try to read another source and say to use (/"something"/g, new) for change all same value, and its still not working
var str = "90807005"
console.log(str.replace(/"0"/g,""))
I want it to be str = "9875";
You can use String.replaceAll or a global flag in your regex:
var str = "90807005"
console.log(str.replaceAll("0","")) //replaceAll
console.log(str.replace(/0/g,"")) //global flag
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:
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);
}
This question already has answers here:
Regex replace('/color[1-9]?[0-9]/g','') not working in JavaScript
(5 answers)
Closed 8 years ago.
Code as below
var data = '<address>Test String & Test</address>';
var cleanData = data.replace('/&/g', "and");
I would like to replace all occurrences of '&'
JSFiddle http://jsfiddle.net/d33wyw6g/
Thanks in advance.
When you use a regex, do not place it between quotes, you can use it directly:
var data = '<address>Test String & Test</address>';
var cleanData = data.replace(/&/g, "and");
This will replace all "&" and not only the first one
Demo here: http://jsfiddle.net/d33wyw6g/2/
try var cleanData = data.replace(/&/g, "and");
Demo
Try this.
data.replace("&", "and");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript equivalent to printf/string.format
replace all occurrences in a string
see title. see the following variable, how can i replace all '%s' with other string?
var s = 'qwer%sqwer%s';
please advice
Use .replace method.
var s = 'qwer%sqwer%s';
s = s.replace(/%s/g, 'other');
Try this:
s.replace(/%s/g,'x')
You can Use replace() function for replacing string
Ex.
var getnewstring = s.replace("%s","YOUR STRING");
Use the .replace method
var s = 'qwer%sqwer%s';
s=s.replace('%s','your string')
document.write(s);