Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Replace using Regex

I want to replace start from @ to º with 0 below is my string

I try its working only for 2 pair but not working for 3 etc pair

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));
like image 335
Brave Man Avatar asked Dec 20 '25 14:12

Brave Man


1 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 also

var r = s.replace(/\s*@[^º]+º\s*/g, 0);
//=> 0+0+0
like image 68
anubhava Avatar answered Dec 22 '25 04:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!