TL;DR: /(?:\s)USA(?:\s)/
, but check out the more sophisticated function at the bottom.
If you want to check if the following character is a space, you just add a lookahead, which looks like this:
const strs = [
'USA is a country',
'They say USA there',
'We are in the USA',
'SOME USABILITY'
];
const pattern = /USA(?:\s)/;
const replacement = 'United States of America ';
console.log(strs.map(str => str.replace(pattern, replacement)));
Note two things:
- Just checking ahead won't work if the word is at the end.
- In the
replace()
function, it'll replace the whole pattern, so you'll need to add the space back to your replacement.
If you want to look at both sides, it's pretty much the same thing:
const strs = [
'USA is a country',
'They say USA there',
'We are in the USA',
'SOME USABILITY'
];
const pattern = /(?:\s)USA(?:\s)/;
const replacement = ' United States of America ';
console.log(strs.map(str => str.replace(pattern, replacement)));
If you want to handle everywhere, you'll also want to add in a check for beginning or end of the string:
const strs = [
'USA is a country',
'They say USA there',
'We are in the USA',
'SOME USABILITY'
];
const pattern = /(?:\s|^)USA(?:\s|$)/;
const replacement = ' United States of America ';
console.log(strs.map(str => str.replace(pattern, replacement).trim()));
Note in this case, we also trim the extra stuff out.
A slightly cleaner method so you don't have to worry about the extra spaces would be to do things in a few steps:
const strs = [
'USA is a country',
'They say USA there',
'We are in the USA',
'SOME USABILITY'
];
const target = 'USA';
const replacement = 'United States of America';
const replaceWord = (str, word, replacement) => {
const pattern = new RegExp(`(?:[^a-zA-Z-]|^)(${target})(?:[^a-zA-Z-]|$)`, 'g');
return (str.match(pattern) || [])
.reduce((result, match) => result.replace(match, match.replace(word, replacement)), str);
};
console.log(strs.map(str => replaceWord(str, target, replacement)));
This is a little more sophisticated of a solution. First, I updated the pattern to not look for just spaces, but anything non-alphabetic (to account for words bumping against things like commas and periods).
Our actually replacement first gets all of the matches (with the extra checks). We then look through it, and for each match, you replace just the original target, then use that whole bit to replace the whole match from the previous.
This is much more flexible.
I also build the pattern as a variable, so you'd be able to replace any word.