'Regular expression for cleaning up strings
[UPDATED]
In an effort to clean up an array of strings that contains people names as well as other titles or user IDs (if present), I need to remove any non-letter characters as well as all characters that are surrounded by parentheses, quotations marks, brackets etc.
For example ["Sean Jackson(he/him)", "Mack $ Adams"]
needs to be changed to ["Sean Jackson", "Mack Adams"]
.
To achieve this I need to write a regular expression that can match such instances but unfortunately my regular expression does not work as intended.
let names = [
'Sean Jackson (he/him)',
'Steven Robinson',
'Mack $ Adams',
'Keira (12345) Nightly',
];
for (i in names) {
names[i] = names[i].replace(/\W*\W/g, '');
}
console.log(names);
The above code returns:
["SeanJacksonhehim", "StevenRobinson", "MackAdams", "Keira12345Nightly"]
Whereas what I want is:
["Sean Jackson", "Steven Robinson", "Mack Adams", "Keira Nightly"]
Solution 1:[1]
So after some digging and with helpful comments from the community I found the following working solution.
Since a space is counted as a non-alphanumerical character itself (\s
is captured by non-alphanumerical regular expression \W
), I had to specify that two consecutive non-alphanumerics need to be on the left side of the target string (hence using \W{2}
).
And then on the right side of the target string there should be any number of characters except for space \S*
.
let names = [
'Sean Jackson (he/him)',
'Steven Robinson',
'Mack $ Adams',
'Keira (12345) Nightly'
];
for (i in names) {
names[i] = names[i].replace(/\W{2}\S*/g, ''); //removes the following patter: [any two non-alphanumerical character followed by any number of any characters except for space]
}
console.log(names);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |