jQuery replacing only one character [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.
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, ""));

Related

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 split a string with camelcase and also with uppercase using regex in javascript? [duplicate]

This question already has answers here:
Regex to split camel case
(14 answers)
Convert camelCaseText to Title Case Text
(26 answers)
Closed 4 years ago.
Here is my string to original:
myCamelCaseSTRINGToSPLIT
expected result:
my Camel Case STRING To SPLIT
Current regex I'm using is
(?=[A-Z])
Any help will be appriciated
Please use this regular expression.
yourString.replace(/([A-Z]+)/g, " $1").replace(/([A-Z][a-z])/g, " $1")
or
yourString.replace(/([A-Z]+)/g, " $1")
Live Demo with two times Replace
Live Demo with single time Replace

REGEX javascript - find two chars set [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
i've to find the sequence of chars ,. inside a json file for example: ":0,.0}},{", so what is the best fitted regex string for it?
i tried /(,.){1,}/g but it doesn't work well.
It's because . is a special character.
const r = /(?:,\.)+/g
console.log('abc,.,.,.def'.match(r)) //[",.,.,."]
you need to escape the .:
/,\./g

replace all occurence of a string with a character using javascript [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 wan to replace all occurrence of a string with single quote but with str.replace it only replaces the first occurrence of the script:
"7<singleQuote>1 inche<singleQuote>s"
Code
var data = "7<singleQuote>1 inche<singleQuote>s"
var output = data.replace("<singleQuote>","'")
Output: 7'1 inche<singleQuote>s
I want to replace <singleQuote> with '.
Use regex with g flag:
var output = data.replace(/<singleQuote>/g, "'");
MDN: String.prototype.replace.

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

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.

Categories

Resources