Madhavan Sundararaj
Madhavan Sundararaj

Reputation: 277

Difficulty in finding correct regex pattern in Angular 7

I have the following regex pattern which is used to replace:

(reply:reply) -> /reply

code:

const reg = new RegExp('\\(reply:([^/]*)\\)');
url = url.replace(reg, '$1');

I am looking for a regex pattern to replace:

(reply:reply/alphanumeric) -> /reply/alphanumeric

Eg: (reply:reply/5acd456) -> /reply/5acd456

I am new to regex patterns, so finding it difficult to come up with a correct solution.

Thanks in Advance

Upvotes: 0

Views: 51

Answers (1)

Smart Manoj
Smart Manoj

Reputation: 5824

const reg = new RegExp('\\(reply:(.*)\\)');
url = url.replace(reg, '/$1');

[^/]* This would only match characters upto /.

(.*)\\) This would capture all characters upto last ) .

Or you could just use slice '/'+url.slice(7,-1)

Upvotes: 1

Related Questions