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)
Related
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
I have this sorta string = "^My Name Is Robert.^"
I want to remove the occurrences of ^ from this string. I tried the replace method like :
replyText.replace(/^/g, '');
But it hasn't any affect. Using the replace without the global works but only removes the first occurrence.
Should I just make a loop and keep looping the string with replace till no more '^' are contained, or is there a better way?
You need to escape the ^ character in RegEx:
replyText.replace(/\^/g, '');
The caret, ^, is a special character in Regex, therefore it has to be escaped with a backslash i.e
replyText.replace(/\^/g, '')
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, ''));
This question already has answers here:
How can I put [] (square brackets) in RegExp javascript?
(8 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I am trying to replace the following in a Regex:-
[company-name]
Using Regex I would expect I could use was to use:-
str.replace(/[company-name]/g, 'Google');
But I know that the regex will replace any matching letter.
How am I able to replace the [company-name] with Google using JS Regex?
Thanks in advance for your help.
But I know that the regex will replace any matching letter.
This is only the case because you have encapsulated your characters in a character class by using [ and ]. In order to treat these as normal characters, you can escape these using \ in front of your special characters:
str.replace(/\[company-name\]/g, 'Google');
See working example below:
const str = "[company-name]",
res = str.replace(/\[company-name\]/g, 'Google');
console.log(res);
You need to escape the starting [ as well, in this case as they are special characters too:
var str = "I am from [company-name]!";
console.log(str.replace(/\[company-name]/gi, "Google"));
str = "[company-name]'s awesome!";
console.log(str.replace(/\[company-name]/gi, "Google"));
you could do it this way :
var txt = "So I'm working at [company-name] and thinking about getting a higher salary."
var pattern = /\[.+\]/g;
console.log(txt.replace(pattern, "Google"))
You should escape special characters like square brackets in this case.
Just use:
str.replace(/\[company-name\]/g, 'Google');
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create regex for checking if password got special characters.
https://www.owasp.org/index.php/Password_special_characters
It looks like this
new RegExp('[!##\$%\^\&*\)\(+=._\'",/<>?[\\\]`{|}~:;-\s]', 'g');
Unfortunately is also catching bare words: reg.test('word') it returns true.
Whats wrong with my regex?
You didn't escape properly. RegExp object could receive a regular expression as a string but escaping matters: it needs double backslashes.
For now [;-\s] is equal to [;-s] which includes 57 characters:
[_;?\[\]#\\`\^<->aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsS-Z]
However, it should be [;\-\\s].
You can use negative/inverse logic and test against any character that is not a number or a letter.
Using [^A-Za-z0-9] where caret (^) matches everything except A-Za-z0-9.
const regex = /([^A-Za-z0-9]|[.\p{L}])/gm;
console.log('word: ' + regex.test('word'));
console.log('word!: ' + regex.test('word!'));
console.log('!£2word!: ' + regex.test('!£2word!'));
console.log('!ą,ć,ó!"£wordąćó: ' + regex.test('!ą,ć,ó!"£wordąćó'));
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I have the following code:
var fileName = "C:\fakepath\a.jpg";
fileName = fileName.replace(/.*(\/|\\)/, '')
and this will return just the a.jpg as intended but I don't understand how it knew to replace the the characters between both of "\" such as the substring "fakepath". From what I see it should just replace the first character "C" because of the period and then any appearances of "/" or "\" with "".
The . means any character.
The * means zero or more.
So .* matches from the start of the string to the point where matching any more characters would prevent the rest of the regular expression from matching anything.