0

I didn't find a solution inside regex's documentation for my current problem. I'm using javascript, html. My code is like this:

var text = 'This [close](animal) is a dog';

I want to get this using regex:

'This {animal} is a dog';

what I mean, i want to have 'close' replaced with { and }. I know, there's a solution like:

var res = text.replace('[close](','{').replace(')','}');

but in my case, I have many rules and I don't want to duplicate that line to do so. Sometimes I'm using other replacement like '[ xxxxx ]'. Any idea? Thank you!

3
  • 2
    I'm afraid the question isn't clear as it currently stands. Perhaps examples of those other "rules"? In particular, do they change what you do with animal? Commented Oct 30, 2018 at 8:47
  • @T.J.Crowder it's actually quite clear :-) Commented Oct 30, 2018 at 8:48
  • @IslamElshobokshy - It actually isn't. Commented Oct 30, 2018 at 8:48

3 Answers 3

3

You may use

var text = 'This [close](animal) is a dog';
console.log(text.replace(/\[[^\][]*]\(([^()]*)\)/g, '{$1}'));

See the regex demo.

Details

  • \[ - a [ char
  • [^\][]* - 0 or more chars other than [ and ]
  • ]\( - a ]( substring
  • ([^()]*) - Capturing group 1: any 0 or more chars other than ( and )
  • \) - a ) char.

The {$1} replacement is the contents of the capturing group enclosed with braces.

If you can only have two values - close and open - inside [...], and replace close with {...} and open with }...{, you may use

var text = '[open](animal)This [close](animal) is a dog';
console.log(text.replace(/\[(open|close)]\(([^()]*)\)/g, function($0, $1, $2) { 
  return $1==='close' ? '{'+$2+'}' : '}'+$2+'{';})
);

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

2 Comments

it works perfectly but if i change [close] with [open], it returns the same result, that's not good, e.g: i want to have }animal{ with [open]
@pitiful Please provide a sample string with expected output. Note you may replace [^\][]* with open or close, as you wish, but there can be better solutions if you explain the scenario exactly. See updated answer.
0

Don't forget, that you can pass custom regex in Array.prototype.replace. In your case it would be text.replace(/[close](/g,'{'). Full solution of your question will look like:

var res = res.replace(/\[\w+\]\((.*)\)/, (a, b) => {
  console.log(a, b);
  return `{${b}}`;
});

The brackets around .* used to 'capture' animal inside variable b

Comments

0

Thank you Wiktor, I'v found a solution by what you said

var res0 = text.replace(/\[close]\(([^()]*)\)/g, '{$1}');
var res1 = text.replace(/\[open]\(([^()]*)\)/g, '}$1{');

Sorry if i did miskates, i'm not used to english expression so :-)

2 Comments

I have included a solution to do that in a single step. See my answer.
well explained and solved my problem, Thank you very much

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.