Javascript Replace with Regex

I want to replace the beginning with @

by Âș

with 0

below is my line

I try to only work for 2 pairs, but doesn't work for 3 pairs, etc.

Example 01:

Input data

@ 8 ~ CÂș + @ 9 ~ CÂș

Output

0 + 0

Example 02:

Input data

@ 11 ~ CÂș + @ 12 ~ P1Âș - @ 13 ~ FÂș

Output

0 + 0 + 0

Below is my code

var tempRes = "@11~CÂș + @12~P1Âș - @13~FÂș";
tempRes = tempRes.replace(/@[0-9]~[A-Z]Âș/i,parseFloat(0));

      

+3


source to share


3 answers


You can do:

var s = '@11~CÂș + @12~P1Âș + @13~FÂș'
var r = s.replace(/@[^Âș]+Âș/g, 0);
//=> 0 + 0 + 0

      




EDIT: To remove spaces as well

var r = s.replace(/\s*@[^Âș]+Âș\s*/g, 0);
//=> 0+0+0

      

+3


source


you can use this regex to catch what you want

@.+?Âș

      

try this demo

Demo



to remove spaces use this regex

\s*@.+?Âș\s*

      

try demo

var input = '@11~CÂș + @12~P1Âș + @13~FÂș'
var output = input.replace(/\s*@.+?Âș\s*/g, 0)

      

0


source


I would use this regex if you have multiple lines in your input:

[ ]*@[^Âș\n]*Âș[ ]*

      

Watch the demo

Explanation:

  • [ ]*

    - Extra space (s)
  • @

    - symbol @

  • [^Âș\n]*

    - 0 or more characters other than Âș

    , and a line break
  • [ ]*

    - Extra space (s)
0


source