Skip to content
Advertisement

Remove words when match with first 3 or 4 characters

JavaScript

I need to remove the words in the sentance which starts with sst or abc

I have a sentence in this way:

JavaScript

I need to remove those words from the above two sentences. I tried with regex sub module but not working

JavaScript

It is removing the word when there is a space only

JavaScript

Advertisement

Answer

You can use

JavaScript

See the regex demo. Details:

  • s* – zero or more whitespace chars
  • (?:abc|sst) – either abc or sst
  • w* – zero or more word chars. Replace with [^Wd_]* to match any Unicode letters or [a-zA-Z]* to only match ASCII letters.

See a Python demo:

JavaScript
Advertisement