Osman
Osman

Reputation: 1771

Trouble with regex

This should be simple to someone who is good with regex:

I have a string like var str="<a class='removable'>$10 STNA Workbook <br/></a>";

and I am able to extract the number which was my first issue. Now the more difficult task is extracting the, in this case "STNA Workbook". However the issue is this statement can be 1, 2, 3, 4 or however many words long. So i need to match everything between the number and < br/>

What my last attempt looked like var patt1=(\d)(.*?)\[<\];

also a quick explanation of the regex parts would be greatly appreciated but not absolutely necessary as I am trying really hard to learn this. any help would be great thanks.

Upvotes: 1

Views: 46

Answers (1)

Hemlock
Hemlock

Reputation: 6210

You were pretty close.

var matches = str.match(/(\d+)(.*?)</);

matches[1] will contain the number and matches[2] will contain the words

You know what the \d (matches digits) is. The (.*?) is a non-greedy match for any character and < ends the non-greedy match

Upvotes: 1

Related Questions