0

In javascript, I have a string like substringof('para1',para2) and substringof('para3',para4) and xxx and xxx or other strings and I want to convert it to contains(para2,'para1') and contains(para4,'para3')

so, what I need to do is to swap the 2 parameters inside substringof and then replace the substringof with contains

thanks

2 Answers 2

4

Something like

string.replace(/substringof\(([^,]+),([^)]+)/, "substringof($2,$1");
  • ([^,]+) Matches anything other than a , captures in group 1, $1. This matches the first parameter.

  • ([^)]+) Matches anything other than a ) , captures in group 2, $2. This part matches the second parameter.

Regex Demo

Example

"substringof('para1',para2)".replace(/substringof\(([^,]+),([^)]+)/, "substringof($2,$1"));
// Outputs
// => substringof(para2,'para1')
Sign up to request clarification or add additional context in comments.

Comments

1

This is what you need to do. Hope it helps.

var str = "substringof('para1',para2) and substringof('para3',para4)";    
var replacedStr = str.replace(/substringof\(([^,]+),([^)]+)/g, "contains($2,$1");

1 Comment

It will do the swipe and replace for all the occurences in the string.

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.