user8085571
user8085571

Reputation:

Javascript REGEX for deleting char only in string start and end, not in the middle

I need to remove the "+" chars in some strings.

I'm using this regex:

string.replace(/\+/g, "");

The problem is when i have strings like: "+++++test+string+++" it obviously gets transformed to teststring and the correct result should be test+string.

So what i need is to figure out a regex that removes the "+" char, only at the start and end of the string.

Thanks!

Upvotes: 1

Views: 102

Answers (1)

Jan
Jan

Reputation: 43169

You may use

^\++|\++$

See a demo on regex101.com.


In JavaScript:

string.replace(/^\++|\++$/g, "");

Upvotes: 2

Related Questions