Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to use the replace function in a forEach loop but it's not working:
var x = [".em", ".one"];
x.forEach((val, index) => {
console.log(val.replace(".", "\."));
});
The issue is because the \ character is the escape character in JS. If you want to output an actual \ in the string, you need to use two of them:
var x = [".em", ".one"];
x.forEach((val, index) => {
console.log(val.replace(".", "\\."));
});
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am willing to know how can I check if the a phone number contain the following prefixes +44, 0044 or 0 and if so it will be removed? I know I to remove a number of characters or a substring but how do I check if that substring is in the beginning?
Thanks in advance
You could with regex /^(\+44|0044|0)/g
function rem(str){
return str.replace(/^(\+44|0044|0)/g,'')
}
console.log(rem('0044987987'));
console.log(rem('04478687'));
console.log(rem('+447783'));
You can use indexOf to check the index of those prefix.
"+44-----".indexOf("+44") // returns 0
That should be enough for what you want.
You can simply test against a regex pattern.
Or use it for a replace.
const re_tel44 = /^[+]?0{0,2}(?:44)?(\d{5,})$/;
let phonenumber = '+4412345';
console.log(re_tel44.test(phonenumber));
if(re_tel44.test(phonenumber))
phonenumber = phonenumber.replace(re_tel44, '$1');
console.log(phonenumber);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want the text which is outside of brackets, for eg.
Text is - Outside (inside)
and what I expect is - Outside
Can someone please help me to achieve this.
You can use slice & use indexOf to get the first (. This will extract all the characters before first (
let str = 'Outside (inside)'
let substr = str.slice(0, str.indexOf('('));
console.log(substr.trim())
If you wanted to remove all bracketed text from the string you could use
let str = 'Outside (inside)test(d 342 dd3d)dd(t423t t)dd()fasf(fsdfds32dfs)';
console.log(str.replace(/(\([\w\d ]*\))+/g, ''))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Could someone with more regex experience than me help me out?
return path.replace(/\//g, '.').replace(/^\./, '');
I have found this regex in a js file within a giant app. The JS when run through npm node-minify or any of the others sees it as a comment and turns it into this:
return path.replace(/\g, '.').replace(/^\./, '');
I get the first bit is replacing all \ with a . and the second bit trims any leading . from the string. Can i change this so the regex pattern is wrapped in quotes?
Just use the RegExp constructor and quote your pattern.
const path = '/usr/bin/env';
const matchSlash = new RegExp('/', 'g');
const translate = path => path.replace(matchSlash, '.').replace(/^\./, '');
console.log(translate(path));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i have string which is
testVariable--423h33c7uhyga5tjk
now i want to replace the above string with
testVariable
by using javascript replace function.
Use String#split method.
console.log(
'testVariable--423h33c7uhyga5tjk'.replace('--')[0]
)
Or with String#replace method.
console.log(
'testVariable--423h33c7uhyga5tjk'.replace(/--.*/, '')
// or including multiline
// .replace(/--[\s\S]*/, '')
)
While Pranav's method work, if you really need/Want to use the replace function, you could use regex:
var variable = 'testVariable--423h33c7uhyga5tjk';
console.log(variable.replace(/--.+$/, ''));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to split string with several char
i have to split a scope when there are spaces, comma, dash, etc..( ponctuation) and when words are concatenated ( without a space between variable)
For exemple
testOne="{{test.test}} {{test.test}}{{test.test}}";
The expected output is
"test.test test.testtest.test"
(There are two comma between the first and the second text.text)
You can use Regular Expression to get the result what you wanted.
testOne = "{{test.test}} {{test.test}}{{test.test}}";
console.log(testOne.match(/{{.*?}}/g).map(function(item) {
return item.replace(/[{}]/g, "");
}));
# [ 'test.test', 'test.test', 'test.test' ]
You can achieve your desired output with a simple replace
testOne.replace(/{{(.*?)}}/g, '$1');
// "test.test test.testtest.test"
Remember to set this back to testOne if you want it kept as that variable