patternjavascriptMinor
Multiple regex replacements
Viewed 0 times
regexreplacementsmultiple
Problem
I receive a string with a lot of characters that I don't need and I am trying to remove them and replace them with characters that I am able to work with. My current structure has me redefining the
I define the date then remove the unwanted characters then append the date to the "clean" string with and underscore:
This does work but I am curious if there is a better way.
var multiple times which I feel is not efficient and can probably be done better. Please let me know of a more effective way I can do this.I define the date then remove the unwanted characters then append the date to the "clean" string with and underscore:
var d = Date.now()
var article = a.replace(/ |\./g, "_")
article = article.replace(/\r?\n|\r/g,"")
article = article.replace(/\$|\#|\[|\]/g, "")
article = d + "_" + articleThis does work but I am curious if there is a better way.
Solution
First, since you already did so inside of your two last expressions, with the same replacement:
I'm puzzled why you didn't simply put both in a unique regexp:
Then to integrate with the 1st one, you might choose to:
-
join the two distincts replacements in a single line:
-
or use a map approach, either suche the one pointed by @greybeard's link, or like this way (even if it might look a bit too sohpisticated for only two cases):
The most interesting aspect in this latter solution is that it may be easily expanded if more replacements are needed.
EDIT following a good suggestion from @Niet the Dark Absol.
As soon as there are several unique characters to look for, with the same replacement, this kind of regexp
Any of the above proposed solutions can be improved this way, so the latter one becomes:
article = article.replace(/\r?\n|\r/g,"")
article = article.replace(/\$|\#|\[|\]/g, "")I'm puzzled why you didn't simply put both in a unique regexp:
article = article.replace(/\r?\n|\r|\$|\#|\[|\]/g, "")Then to integrate with the 1st one, you might choose to:
-
join the two distincts replacements in a single line:
var article = a.replace(/ |\./g, "_").replace(/\r?\n|\r|\$|\#|\[|\]/g, "")-
or use a map approach, either suche the one pointed by @greybeard's link, or like this way (even if it might look a bit too sohpisticated for only two cases):
var replacements = new Map([
[/ |\./g, '_'],
[/\r?\n|\r|\$|\#|\[|\]/g, '']
]),
article = a;
replacements.forEach(function(value, key){
article = article.replace(key, value);
});The most interesting aspect in this latter solution is that it may be easily expanded if more replacements are needed.
EDIT following a good suggestion from @Niet the Dark Absol.
As soon as there are several unique characters to look for, with the same replacement, this kind of regexp
/(a|b|c)/ can be replaced by /[abc]/, which is both simpler and more efficient!Any of the above proposed solutions can be improved this way, so the latter one becomes:
var replacements = new Map([
[/[ .]/g, '_'],
[/[\r\n$#[\]]/g, '']
]),
article = a;
replacements.forEach(function(value, key){
article = article.replace(key, value);
});Code Snippets
article = article.replace(/\r?\n|\r/g,"")
article = article.replace(/\$|\#|\[|\]/g, "")article = article.replace(/\r?\n|\r|\$|\#|\[|\]/g, "")var article = a.replace(/ |\./g, "_").replace(/\r?\n|\r|\$|\#|\[|\]/g, "")var replacements = new Map([
[/ |\./g, '_'],
[/\r?\n|\r|\$|\#|\[|\]/g, '']
]),
article = a;
replacements.forEach(function(value, key){
article = article.replace(key, value);
});var replacements = new Map([
[/[ .]/g, '_'],
[/[\r\n$#[\]]/g, '']
]),
article = a;
replacements.forEach(function(value, key){
article = article.replace(key, value);
});Context
StackExchange Code Review Q#117672, answer score: 4
Revisions (0)
No revisions yet.