Edit: You've added a comment which changes things. I'll pick up the replacement scenario after the line below the original answer.
Original answer:
You're on the right track with the regular expression, you just need to say what it is following the @ that should be removed. For instance, if it's just English alphabetic characters:
str = str.replace(/@[A-Z]+/gi, "");
var str = "Hello STO, This is a test string @text and I need all the strings with @strings to be replaced.";
str = str.replace(/@[A-Z]+/gi, "");
console.log(str);
If you want to include digits or other characters, just adjust the [A-Z] accordingly.
If you want to remove the whitespace (if any) after it, add \s* to the regex:
str = str.replace(/@[A-Z]+\s*/gi, "");
var str = "Hello STO, This is a test string @text and I need all the strings with @strings to be replaced.";
str = str.replace(/@[A-Z]+\s*/gi, "");
console.log(str);
Sterling Archer raised an interesting question:
is there way to read to the next instance of a space instead of checking for specific characters?
So basically, instead of listing the characters that might follow the @, we list the characters that would terminate whatever follows the @ (like a space, comma, etc.). We'd do that with a negated character class:
str = str.replace(/@[^\s\t,.:?]+/gi, "");
var str = "Hello STO, This is a test string @text and I need all the strings with @strings to be replaced.";
str = str.replace(/@[^\s\t,.:?]+/gi, "");
console.log(str);
And again trimming following whitespace:
var str = "Hello STO, This is a test string @text and I need all the strings with @strings to be replaced.";
str = str.replace(/@[^\s\t,.:?]+\s*/gi, "");
console.log(str);
That removes it. In a comment added later, though, you've said:
no, not trying to remove it. trying to replace it
In that case, depending on what the replacement is, you might use a function to determine what to replace the tokens with:
str = str.replace(/@[A-Z]+/gi, function() {
return /*the replacement*/;
});
The function receives the overall matched string. If you want to get the word following the @ without the @, you can use a capture group. The values of capture groups are listed after the overall match. So:
str = str.replace(/@([A-Z]+)/gi, function(overallMatch, capture0) {
return /*the replacement*/;
});
For instance, we could have an object we look up replacements on:
var replacements = Object.assign(Object.create(null), {
text: "**TEXT**",
strings: "**STRINGS**"
});
var str = "Hello STO, This is a test string @text and I need all the strings with @strings to be replaced.";
str = str.replace(/@([A-Z]+)/gi, function(overallMatch, capture0) {
var rep = replacements[capture0];
return rep || "**MISSING**";
});
console.log(str);