Miki
Miki

Reputation: 2517

Replacing characters inside URL parameters via regular expression in javascript

I want to replace some diacritic characters inside URL parameter values using regular expression for finding character values.

For single character/word I make replacement with line:

url = url.replace(/null/g, "");

I can use this regular expression to find parameter values:

/\=[a-zA-Z0-9šŠđĐčČćĆžŽ]*\&/g

How to make replacement in single line (if possible)?

For example

INPUT: http://localhost:8080/page?param1=svašta&param2=nešto&param3=trebam

OUTPUT: http://localhost:8080/page?param1=svata&param2=neto&param3=trebam

Upvotes: 0

Views: 775

Answers (2)

Bergi
Bergi

Reputation: 665130

You can do

url = url.replace(/\=[a-zA-Z0-9šŠđĐčČćĆžŽ]*\&/g, function(match) {
    return match.replace(/[šŠđĐčČćĆžŽ]/g, "");
});

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89604

You don't need a regex for that, you can use encodeURI

url = url.replace(/null/g, "");
url = encodeURI(url);

Upvotes: 0

Related Questions