3

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));
3
  • In one output, you have spaces; in the other, you don't -- but there are spaces in both inputs. Do you want them or not? Commented May 18, 2015 at 10:15
  • @No spaces,both will without space Commented May 18, 2015 at 10:17
  • Okay. Please use the "edit" link to correct the question. Please also note the formatting fixes I applied for you: Taking the time to make sure your question is clearly formatted is important. There's an entire toolbar full of things to help you, and a lot of help available from the [?] button. Commented May 18, 2015 at 10:18

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

[ ]*@[^º\n]*º[ ]*

See demo

Explanation:

  • [ ]* - Optional space(s)
  • @ - A @ symbol
  • [^º\n]* - 0 or more symbols other than º and a line break
  • [ ]* - Optional space(s)

Comments

0

you can use this regular expression to catch what you want

@.+?º

try this demo

Demo

to remove spaces use this regex

\s*@.+?º\s*

try this Demo

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

Comments

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.