replace does not replace every character in a sentence [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does javascript replace only first instance when using replace?
How do I replace all occurrences of “/” in a string with “_” in JavaScript?
I want to replace every - in a sentence but it only replace the first -. Here is my code:
var string = 'this-is-a-line-of-words';
alert(string.replace('-', '/'));​
Why does it only replace the first character I want to replace? jsFiddle demo.
Thanks in advance.

Use a global regex:
string.replace(/-/g, '/')

Please use string.replace(/-/g, '/'). And check this doc please.

Related

Remove single quotes and last comma in jquery string using Regex [duplicate]

This question already has answers here:
JavaScript REGEX Match all and replace
(2 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a data
'abc','def','ghi',
I want to remove the single quote on all character and want to remove only the last comma, example like below
abc,def,ghi
How would i achieve that using regex for javascript?
I tried using this regex
.replace(^\'|,\s*$,"");
But seems like it is only removing the first quote as shown below
abc','def','ghi',
I am not very good in regex, i appreciate any help that i can get. Thanks
try this:
.replace(/\'|,$/g, "");
the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match
There is an easy way, use the $ operator
.replace(/'|,$/g, '')
Can you please check replace(/'|(,)$/g," ") and it should work.

How to use replaceAll() in JavaScript? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I have string and there are alot of character which is "\" so I want to remove all of them but when I use
string.replace("\\","");
it removes just first character. There is no replaceAll in reactnative. How can I do that ?
Use regex in your replace.
Example:
const string = "aaabbbcccaaa";
// Removes first "a" only
console.log(string.replace("a", ""));
// Removes all "a"
console.log(string.replace(/a/g, ""));
You have to use a regex with the g modifier whichs means globally, that will apply the regex as many times as necessary. You can use a tool like this to build and test your regex.
console.log('hello\\world\\'.replace(/\\/g, ''));

How to use end of string in square brackets in javascript regex? [duplicate]

This question already has answers here:
Using $ anchor inside a character class does not work
(2 answers)
Closed 3 years ago.
In js regex, I have
[\.\?!][\s$]
what I want to do is match
literal dot, or literal question mark or explanation mark
then
either 1 whitespace character or, be at the end of the string.
However the regex above, tries to match the literal $.
Does anyone know how to fix this?
Thanks
Try this Regex:
[.?!](?:\s|$)
Click for Demo
Explanation:
[.?!] - matches either a . or a ? or a ! literally
(?:\s|$) - matches either a white-space or the End-of-line

jQuery replacing only one character [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 5 years ago.
jQuery:
console.log("'Wild.jpg'".replace("'", ""));
Output:
Wild.jpg'
How can I resolve this and make the output as Wild.jpg?
You should use RegEx with the g modifier instead of an ordinary string replacement:
console.log("'Wild.jpg'".replace(/'/g, ""));
Try this.. here we gave /g to replace all.
console.log("'Wild.jpg'".replace(/'/g, ""));

How can I use regex to replace all $#8211; with – in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I found this question/answer:
Use JavaScript regex to replace numerical HTML entities with their actual characters
I just need to replace the one entity though. How can I match that specific pattern with a regex?
I don't know much about regex so I've done this:
.replace('–', '–')
But it obviously only replaces the first instance.
Thanks,
Thomas
The replace method only replaces the first occurance when you are using a string. Use a regular expression, so that you can specify the global flag g:
.replace(/–/g, '–')
.replace(/–/g, '–')
the g flag means global so it replaces all instances.

Categories

Resources