Post

2 followers Follow
0
Avatar

How to extract Numbers from String?

For Example

In my Comment field I will get a comment from our Team leaders stating like “Team Strength = 15 Nos “

I want to take this 15 from above string and planning to use for my Calculation

I have used some codes from supports but it hasn’t worked for me.

 

Please advise

Ravikishan Bhat

Please sign in to leave a comment.

7 comments

0
Avatar

Hi Ravikishan,

in this use case where there is only one number ("15") in the text: 
@comment field.replace(/[^0-9]/g,"")
That replaces everything but digits.
If the format is always the same, e.g = followed by the number you can also use: 
@comment field.split("= ").pop().split(" ").shift()

Both ways return 15 - but it's technically a string (text) not a number, you can't calculate with it, e.g. that 15 plus 1 will return 151, not 16. You must parse it to a number first:

Number(@comment field.replace(/[^0-9]/g,"")) || 0

Rainer

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Hi Rainer

 

I used your suggestion as per attached screen shot result I got " 0" Always Please check 

I want to get 15 as output in this example 

Ravikishan Bhat 0 votes
Comment actions Permalink
0
Avatar

Hi Rainer 

I tried following formula as well but it wont worked 

 

str = @Member Count.replace(/(<.*>)|( )/g,"").replace(/( )|([_\n\r\*\\])|(&nbsp;)/g, " ");

Number(str. replace(0 ||/[^0.9]/g,""))

Ravikishan Bhat 0 votes
Comment actions Permalink
0
Avatar

Sorry, I had a typo in my code 
must be [^0-9] not [^0.9] (don't know why it had a dot in it) 

Number(@Member Count.replace(/[^0-9]/g,"")) || 0

But 

Number(@Member Count.split("= ").pop().split(" ").shift())

should work too. 

Your formula
str = @Member Count.replace(/(<.*>)|( )/g,"").replace(/( )|([_\n\r\*\\])|(&nbsp;)/g, " ");
Number(str. replace(0 ||/[^0.9]/g,""))
doesn't make any sense. 

Rainer

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Hi Rainer, 

 

I tried this code as well but Result is "0" 

 

var Can= (str = @comment Filed.replace(/(<.*>)|( )/g,"").replace(/( )|([_\n\r\*\\])|(&nbsp;)/g, " "));

var Van = Number(Can.replace(/[^0.9]/g,"")) || 0;

Van;

Ravikishan Bhat 0 votes
Comment actions Permalink
0
Avatar

Hi Rainer, 

Number(@Member Count.replace(/[^0-9]/g,"")) || 0

This Code Worked for Me Thanks a lot
Ravikishan Bhat 0 votes
Comment actions Permalink