Dren
Dren

Reputation: 319

strip a regex character match from a string

I would like to strip specific character (the ampersand) match from any position on a string. An example of strings are;

&Discounts
Dis&counts
Discounts&
& Discounts
Discounts &
&

I use the regex &(?=\s|[a-zA-Z1-9]) but it can only match the ampersand at the beginning and the middle of the string. How can I also match if the ampersand is at the end of the string. And should be able to exclude match &. Modified my regex to &(?=\s|[^amp;]). It was able to exclude the string & but still not able to match the ampersand at the end of the string.

Upvotes: 2

Views: 474

Answers (1)

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

You just need to catch

[&](?!amp;)

and replace by empty

Example

Upvotes: 2

Related Questions