AabinGunz
AabinGunz

Reputation: 12347

JavaScript regex matching

I have some variable

var jdbcurl="jdbc:oracle:thin:%2F%2Finnova:1521%3BServiceName%3Dorcl%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue"

alert(jdbcurl.match(/:[\d]+/));    //gives me :1521

How can I get jdbc:oracle:thin, innova, 1521 & orcl out of jdbcurl var?

Update

You can experiment here (if needed)

Upvotes: 3

Views: 14750

Answers (4)

1983
1983

Reputation: 5963

Use decodeURIComponent() first, then split on semicolons. Don't make it hard on yourself!

Upvotes: 0

maxRoald
maxRoald

Reputation: 99

you could also try this for better url readability during regexp maintenance if you have to parse several urls:

var jdburl = unescape("jdbc:oracle:thin:%2F%2Finnova:1521%3BServiceName%3Dorcl%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue");

var myRegExp = new Regexp('([a-z:]+)://(\\w+):(\\d+);ServiceName=(\\w+);');

var matches = myRegExp.exec(jdburl);

Upvotes: 5

Zack Burt
Zack Burt

Reputation: 8455

var jdbcurl="jdbc:oracle:thin:%2F%2Finnova:1521%3BServiceName%3Dorcl%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue"
var myregex = /([a-z:]+):%2F%2F([a-z]+):(\d+)%3BServiceName%3D([a-z]+)%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue/
var matches = myregex.exec(jdbcurl);
// jdbc:oracle:thin is in matches[1], innova is in matches[2], 1521 is in matches[3], and orcl is in matches[4]

Upvotes: 10

pagid
pagid

Reputation: 13877

I'd say jdbcurl.split(/(%..)/) would be a start - and then you could check the elements on whether to keep them or split them even further.

Upvotes: 0

Related Questions