Link to home
Start Free TrialLog in
Avatar of rgarimella
rgarimella

asked on

String SubString Manipulation

Hi Folks,

I have a javascript variable which has a value of "ab_03_01_0010.htm"

I need to extract 03 out of the string and put it in another var. The naming convention will be the same always.

var stringVal = "ab_03_01_0010.htm" ;

I need the solution so that I get
var subStringVal = "03"

Thanks

RG
Avatar of abel
abel
Flag of Netherlands image

just from the top of my head, try this:

   var subStringVal = stringVal.replace(/^[^_]+_([^_]+)_.*$/, '$1');
tested it, works for me. The construct [^_]+ is used in the regular expression to capture anything NOT containing an underscore, which is necessary to get to the second "element" containing your "03" (which is caught the same way).
Avatar of rgarimella
rgarimella

ASKER

sorry there was a bug in my code

var stringVal = "ab_03_01_0010.htm" ;

How do i capture

var subStringVal = "03_01"

The filename convention will be the same
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Much Appreciated for your help

RG
You're welcome, glad I could be of some help :)

-- Abel --