javascript replace instance of string [duplicate] - javascript

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 5 years ago.
I want to replace all instance of particular string using javascript/jquery.
My source string is '\\n' and my desired output is '\n'.
this should happen at all instance with in a string.

var str = "hii All, \\n I am sambo. \\n Thank you for listening.";
var res = str.replace(/\\n/gi, "\n");
console.log(str);
Use replace with regex.

Related

Is there is a way to replace multiple character with replace function? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
my replace function only replace one character.
My Code example
const string = "What,Yo,Yes"
string.replace(","," ")
Console
"What Yo,Yes"
Something like this:
const string = "What,Yo,Yes";
console.log(string.replace(/,/g," "));
Alternatively, you can use replaceAll, like this:
const string = "What,Yo,Yes";
console.log(string.replaceAll(","," "));

How to replace all / with - from string [duplicate]

This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.

Javascript split string by another string and include that another string that was used to split it into result [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Closed 5 years ago.
If I have this string: "MyStringToForSplitting" and I want to split it by this word "ring".
If I do this: "MyStringToForSplitting".split('ring')
I will get array that contains this elements:
MyS
ToForSplitting
I can't figure out best way to split it and include 'ring' in resulting array like this:
1.MyS
2.ring
3.ForSplitting
You can use regex and capture the split pattern:
console.log("MyStringToForSplitting".split(/(ring)/));
var s = "ring";
var p = new RegExp("(" + s + ")")
console.log("MyStringForSplitting".split(p))

.replace all variables in string [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 8 years ago.
I want to replace all the words that match with a variable in a string, but it doesn't work. I use this code:
var findStr = "hello hi, test, hi";
var textSearch = "hi";
findStr = findStr.replace(textSearch,'<span>'+textSearch+'</span>');
It has to change the color of hi but only the first hi changes, the second one doesn't.
The replace method is what you're looking for.
For example: someString.replace("String you want to replace", "with this string")

how to get the value(ID) using javascript substring? [duplicate]

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

Categories