javascript regex replace
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
//if pattern is regular expression all matches will be replaced
//output: "cow cow cat rat"
let re = /apples/gi;
let str = "Apples are round, and apples are juicy.";
let newstr = str.replace(re, "oranges");
console.log(newstr)
output:
"oranges are round, and oranges are juicy."const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result; // => 'goose goose go'str.replace(/abc/g, '');str = str.replace(/abc/g, '');const p = 'hello world ! hello everyone ! ';
const regex = /hello/gi;
console.log(p.replace(regex, 'good morning'));
// expected output: "good morning world ! good morning everyone !"
console.log(p.replace('hello', 'good evening'));
// expected output: "good evening world ! hello everyone !"
Also in JavaScript:
- change windlow location relitave to current one
- javascript string single or double quotes
- nodejs optimizing compuler try catch
- 1.1 0da14962afa287e5ba55c7d30c902392.cloudfront.net w
- javascript check if file exists on server
- javascript throw new error
- overlapping times javascripts html django
- deduplicate array javascript
- how to format unix timestamp javascript
- Exceeded maximum budget Budget 10 kB was not met by 478 bytes with a total of 10.5 kB.
- push values to state array class react
- js calculate date difference
- how to print diamond pattern in javascript
- mongodb check for array not empty query
- javascript add button to div
- pageyoffset jquery
- mvc form client side validation result callback
- moment get month day
- expo navigation
- Angular Quick Tip: Binding Specific Keys to the Keyup and Keydown Events
- addclass removeclass jquery
- js not instanceof
- streami node js
- jquery enable disable textbox