opensas
opensas

Reputation: 63395

replacing characters with javascript regular expression

I want to replace underscores with slashes when they are before a four digit number, for example

res_234_2010-xxx -> res_234/2010-xxx
res_12345_1999-yyy -> res_12345/1999-yyy

(In fact, the four digit number should be a valid year, startin with 19xx or 20xx)

Is there some way to achieve it with .replace method, or shall I do it programmatically?

Upvotes: 0

Views: 43

Answers (1)

hwnd
hwnd

Reputation: 70722

You can use the following regular expression:

var result = str.replace(/_((?:19|20)\d{2})\b/g, '/$1');

Regular Expression Explanation

Upvotes: 2

Related Questions