-3

I'm trying to remove every string with an @ sign attached to it using jquery.

for EXAMPLE, the string looks like this:

Hello STO, This is a test string @text and I need all the strings with @strings to be replaced.

So in the example above, I need to replace the @text and @strings .

The strings with @ signs are dynamic so I can't do simple string replace like this:

Mystring.replace(/@text/g, "");

Can someone please let me know if this is possible?

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
David Hope
  • 1,426
  • 4
  • 21
  • 50
  • 2
    jQuery is irrelevant here – Sterling Archer Apr 10 '17 at 15:55
  • Are you just looking to remove "any word that starts with `@`"? You can use regex for that. – Tyler Roper Apr 10 '17 at 15:56
  • @Santi, no, not trying to remove it. trying to replace it. – David Hope Apr 10 '17 at 15:56
  • Your provided example shows you removing it, and you haven't told us what you want to replace it with. Is there any "formula" for how the matched element gets replaced? Otherwise, you may have to make a relational object to say "A" gets replaced with "B", so-on and so-forth. – Tyler Roper Apr 10 '17 at 15:58
  • @DavidHope This is javascript 101! Many good examples out there! – funcoding Apr 10 '17 at 15:58
  • @funcoding and @MikeMcCaughan - after these comments, it seems OP is asking for something a bit more complicated than his question indicates. The `@test` sections aren't meant to be removed, but they are more like dynamic placeholders, each being replaced with something unique. – Tyler Roper Apr 10 '17 at 16:00

2 Answers2

4

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);
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

While TJ's answer is very well done, I'd just like to provide you an alternative in the event you're looking to have a relational replacement. For each key/value pair in your replacements object, it will replace @key with value.

var str = "My name is @name. My favorite food is @food.";
    
var replacements = {
    "name" : "John Doe",
    "food" : "spaghetti"
};
    
for (var k in replacements) {
    var ph = "@" + k;                          //store "@key"
    var re = new RegExp(ph,"g");               //regex object for @key
    str = str.replace(re, replacements[k]);    //replace with value
}
    
console.log(str);
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56