简体   繁体   中英

Replace a string's special characters using regex

the string looks something like below:
/1/2/3/4 however I want to replace this with ?1=2&3=4 .
I am planning to use REReplace in ColdFusion. Could you suggest me a regex for this ?
I also thought of using loops but stuck either way...
Thanks in Advance

A bit cumbersome without making it more manageable using a loop as @Leigh suggested; but you can use the following on string inputs that contain even occurrences of n/m in the format you described:

var s = "/1/2/3/4/5/6";
s.replace(/^\//,'?').replace(/(\d+)\/(\d+)/g,'$1=$2').replace(/\//g,'&')
// => "?1=2&3=4&5=6"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM