Sunday, 15 January 2012

jquery - How to write regular expression in the following case using javascript? -



jquery - How to write regular expression in the following case using javascript? -

var value = "id=advicon1&class=advicon&page=43&top=2%&left=15%&width=20%&height=10%&rsc=http://www.canon.com.hk/40th/index.html?source=seriesbanner&icon=http://203.80.1.28/flippingbook/dev/frontend/source/adv/tc_bn_314.jpg&alt=cannon adv"

what accomplish from

&rsc=http://www.canon.com.hk/40th/index.html?source=seriesbanner

to

&rsc=http://www.canon.com.hk/40th/index.html?source#seriesbanner

which replace "=" between &rsc , &icon

value = value.replace (/&rsc=%[=]+%&icon/,/&rsc=%[#]+%&icon/);

the above code tried, not working though, how prepare problem ?

i this:

var value = "id=advicon1&class=advicon&page=43&to..."; var startindex = value.indexof("&rsc"); var endindex = value.indexof("&icon"); var head = value.substring(0, startindex); var tail = value.substring(endindex); var body = value.substring(startindex, endindex); var result = head + body.replace(/=/g, '#') + tail;

i don't see advantage in trying whole thing 1 crazy regex.

that create code harder read , less efficient.

better yet, create function can re-use:

// replaces every occurrence of replacethis withthis in input between // startpattern , endpattern. function replacecharactersbetween(input, startpattern, endpattern, replacethis, withthis) { var startindex = input.indexof("startpattern"); var endindex = input.indexof("endpattern"); var head = input.substring(0, startindex); var tail = input.substring(endindex); var body = input.substring(startindex, endindex); var regex = new regexp(replacethis, 'g'); homecoming head + body.replace(regex, withthis) + tail; }

javascript jquery regex

No comments:

Post a Comment