lucas
lucas

Reputation: 503

javascript swap and replace string

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

Upvotes: 0

Views: 156

Answers (2)

Swaprks
Swaprks

Reputation: 1553

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");

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

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')

Upvotes: 4

Related Questions