0

I'm trying to replace the / separator in the value of URL param1 parameter with \, but I can't figure how to do it without changing it for all parameters like param2

For example

param1=abc/def/123&param2=abc/def/123

I would like to become

param1=abc\def\123&param2=abc/def/123

Here is a regex101 example

4
  • Replace (param1=.+?)\/ with \1\\? regex101.com/r/2RRLKf/1 Commented Nov 6, 2019 at 20:02
  • @AhmedAbdelhameed I'm don't understand your solution. It only replaces the first slash, but not the second one and there could be mutiple ones. Here is the updated example Commented Nov 6, 2019 at 20:07
  • My bad, I thought you only wanted to replace the first one. What programming language or regex flavor are you using? Commented Nov 6, 2019 at 20:11
  • I will do this in Java 1.6, and I was wandering if I can avoid iterating the parameters and their values. Commented Nov 6, 2019 at 20:12

1 Answer 1

1

I would split this in 2 simple steps. First dig out "param1=..." substring with regexp, and then just replace "/" with "\" in it. Here's an example in JavaScript:

var str = "param1=qwe/qwe/wer&param2=qd/fs/aw";
var match = str.match(/^(.*)(param1=[a-z\/0-9_]+\b)(.*)$/);
var result = match[1] + match[2].replace(/\//g, "\\") + match[3];
console.log(str, result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.