0

Find Pipe into the (fad(dfad) | dfasd |dada(dafs)) and replace with @ from jquery pattern.

var str = 'da(dd) | dasd(dsa) | dad(asdf) (fad(dfad) | dfasd |dada(dafs)) | adfs(daf)'

str.replace(/([\|])/g,'@'); --- not working.

I want Output: da(dd) | dasd(dsa) | dad(asdf) (fad(dfad) @ dfasd @ dada(dafs)) | adfs(daf)

Please help me for find the result;

user38205
  • 1
  • 2
  • Seems to work just fine for me. http://i.imgur.com/4JiCGk3.png – NewToJS May 17 '17 at 09:28
  • It works for me too. [Here](http://stackoverflow.com/a/17606289/5897779) is a quite extensive answer to what you want to do :) – Ozgar May 17 '17 at 09:29
  • I want output: da(dd) | dasd(dsa) | dad(asdf) (fad(dfad) @ dfasd @ dada(dafs)) | adfs(daf) – user38205 May 17 '17 at 09:30
  • 1
    Will the parenthesis always follow that same pattern? If so I'll make a regex finding the section you want – Ozgar May 17 '17 at 09:31

1 Answers1

0

I tinkered a bit and put the following together which will have the effect you want. Assuming that the pattern of character types will remain the same.

str.replace(/(?:(\(\w*\([^|]*)(\|)([^|]*)(\|)(.*))/,'$1@$3@$5');

Essentially what happens is that it groups the string in to 5 groups:

  1. Before the first '|'
  2. The first '|'
  3. Between the two '|'
  4. The second '|'
  5. The rest

After the grouping it is reconstructured but without groups 2 and 4, replacing these with '@'. Pretty sure this can be done in a prettier way but I atleast this works!

EDIT: Made a more generic solution using look arounds, hopefully this will work for you.

str.replace(/(?=[^\)\(]*(?:\)|\(\w*\)[^\)\(]*(?:\)|\(\w*\)\s*\))))\|/g, '@');
Ozgar
  • 305
  • 2
  • 14
  • It is useful but not for every dynamic series like that: undefined | gj(fg) ( hjfg(hgj) | hfj(hj) | fjh(hgj) ) | hdfg(hdg) | ghdf(ghdg) | hg(dfh) ( gh(hgdf) | ghdf(hfdg) | hgd(hgf) ) | dfsgf(dsg) | fdg(sdfg) ----- not working for this. I need regex for dynamic series . – user38205 May 18 '17 at 05:56
  • I suspected as much. I will take another look at it. – Ozgar May 18 '17 at 07:01
  • Edited my answer with a new regex that should work :) – Ozgar May 18 '17 at 09:38
  • If it solved your problem please accept it as an answer – Ozgar May 23 '17 at 11:03
  • Its working fine but not for dynamic series: test(zxcZX) ( dsafsdf(sdfSDF) | asdfsd(WRWER) | sadfasd(QWEDQRW) ) | asdfa(89) ( asd(WQWERW) | aftaft(SDFSD) ). – user38205 May 24 '17 at 07:28
  • I need for: a(x) | b(y) | c(3) | d(e) ( x(s) | sd(df) | ds(ds)) | ds(ds) | sd ( sd | dfd(ds) | ds(ds)). replace pipe(|) with @ inside every brackets. – user38205 May 24 '17 at 07:32