JeroenvanRensen
1 month ago

JS - Reg ex new value in str replace

Posted 1 month ago by JeroenvanRensen

Hi everyone,

I have got a problem with Regular Expressions. I do have this string:

https://nl.wikipedia.org/wiki/Republikeinwever

And I want to change it to this:

https://nl.m.wikipedia.org/wiki/Republikeinwever

But, nl and Republikeinwever can change, and it should still work.

So this:

https://en.wikipedia.org/wiki/World_War_II

Should become this:

https://en.m.wikipedia.org/wiki/World_War_II

How can I do this in pure JS?

I tried to do it with Regular Expressions, but couldn't solve it.

Something like this?

var newValue = oldValue.replace(
    /https:\/\/([a-z]+).wikipedia.org\/wiki\/([A-Za-z]+)/i,
    https://.m.wikipedia.org/wiki/
);

I hope anyone can help me.

Thanks!

Please sign in or create an account to participate in this conversation.