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.
Related
This question already has answers here:
How to split by commas that are not within parentheses?
(6 answers)
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 3 years ago.
Here is the my string
var string = 'Title:(India OR America) OR Keyword:(Education)';
After splitting with "OR" it is showing
["Title:(India", "America)", "Keyword:(Education)"]
But what I need is the below one.
["Title:(India OR America)", "Keyword:(Education)"]
Could anyone please help on how to split the string to get the required result.
To achieve this you'll need to use a negative lookahead to only find the OR string where it's not contained in parentheses. Try this:
var input = 'Title:(India OR America) OR Keyword:(Education)';
var output = input.split(/(?!\(.*)\s?OR\s?(?![^(]*?\))/g);
console.log(output);
This question already has answers here:
Delete first character of string if it is 0
(17 answers)
the best way to remove the first char of a given string in javascript
(4 answers)
Closed 3 years ago.
I see this article but it's specific to deleting a character if it's a certain number (0, in that case).
I want to remove the first character from a string no matter what it is.
splice() and shift() won't work because they're specific to arrays:
let string = "stake";
string.splice(0, 1);
console.log(string);
let string = "stake";
string.shift();
console.log(string);
slice() gets the first character but it doesn't remove it from the original string.
let string = "stake";
string.slice(0, 2);
console.log(string);
Is there any other method out there that will remove the first element from a string?
Use substring
let str = "stake";
str = str.substring(1);
console.log(str);
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 3 years ago.
I have a string like
abcd/123/xyz/345
I want to replace every "/" with "-" using JavaScript.
The result string should be abcd-123-xyz-345
I have tried,
string.replace("/","-")
But it replaces the first "/" character only. The result is abcd-123/xyz/345
And
string.replace("///g","-");
is not working as well.
Is there any solution for this?
You can use Regex. You need to escape using backslash \ before the /.
A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally
var str = 'abcd/123/xyz/345';
let result = str.replace(/\//g,'-');
console.log(result);
Please try this,
var str='abcd/123/xyz/345'
var newstr=str.split('/').join('-');
console.log(newstr)
This question already has answers here:
Escaping HTML strings with jQuery
(27 answers)
Closed 5 years ago.
In javascript how it's possible to replace all occurrence of &,<,> in a string
I tried this
var str="<>&";
str.replace("&","&").replace("<","<").replace(">",">");
but not able to change even first occurrence
The easiest would be to use a regular expression with g flag to replace all instances:
str.replace(/foo/g, "bar")
This will replace all occurrences of foo with bar in the string str. If you just have a string, you can convert it to a RegExp object like this:
var pattern = "foobar",
re = new RegExp(pattern, "g");
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Replace all plus signs (+) with space in a string
(1 answer)
Closed 5 years ago.
How can I replace multiple (++++++) signs with blank space?
Example: var string = "+++++++++++++++++";
var string = "+++++++++++++++++";
string = string.replace(/\+*/g, '');
console.log(string.length);
const str = 'abc++++++def++++frberbf++fsvsf';
const newstr = str.replace(/(.)\+{2,}/g, ' ');
console.log(newstr);