Search code examples
regexprefix

Regex match any character except given prefix


I am looking for a regex pattern with can do as the title, it's mean with 3 given prefixs "#,@,+", the following character will be ignore until it is whitespace. Example:

"+20 @facebook #hashtag with my love"

will return

"with my love"


Solution

  • You can use this in the following way

    const regex = /[\+|\@|\#][\w\p{P}]*\s+/g;
    const str = '"+20 @facebook #hashtag with my love"';
    const subst = '';
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);
    

    take a look at https://regex101.com/r/K1YQQZ/1