Rajasekhar
Rajasekhar

Reputation: 2455

How to fetch only numeric value in a string using Regex?

I would like to fetch one item in the string,

the main string is : This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting

I would like fetch only "903213712", is there any way to achieve this one using Regex ?

Tried with below regex, it gives 16059636/903213712 == > I'm expecting only 903213712 after slash.

var string = "This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting"

var result = string.match(/\d+/g)

//var resVal = result.toString().split("/");

alert(result);

Upvotes: 1

Views: 706

Answers (1)

prasanth
prasanth

Reputation: 22490

you are almost done \/ is missing => /\/(\d+)/g

Explanation

  1. \/ match the /
  2. \d+ matches a digit (equal to [0-9]) .for more info see the demo regex

Demo regex

var string = "This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting"
var result =/\/(\d+)/g.exec(string)
console.log(result[1]);

Upvotes: 8

Related Questions