views:

553

answers:

2

I have the following regular expression in .Net

(?<=Visitors.{0,100}?"value">)[0-9,]+(?=)

and the following text

<div class="text">Visitors</div> <div class="value">14,000,000</div>
<div class="text">VisitorsFromItaly</div> <div class="value">15,200</div>

I specify in the regex either "Visitors" or "VisitorsFromItaly" and I get the number that specific number of Visitors.

I want to do this in javascript but without using any .replace methods. I just need a plain simple javascript regex. I am new to javascript regex so I have no idea how to convert it. It says that javascript does not support lookbehind and that is the thing that is making my .Net regex working.

Any help would be appreciated. Thanks a lot in advance!!!

A: 

are you getting this data as a string? or do you have access to a DOM object (e.g. AJAX response)?

If you have access to the latter, I wouldn't bother with regex.

scunliffe
Thanks for the reply...I am just using RegexBuddy and not Visual Studio or smth else. I have this text and I want to create a Regex expression to get the number of visitors. I have this in .NET language and it is working perfectly. I need this in javascript now...
ah, ok. I thought when you mentioned javascript that you were running this in the browser. If so, there are a variety of other options.
scunliffe
+2  A: 

I'm not sure I understand you... Do you really need a lookbehind assertion?

Can't you just match something like the following?

myRegex = /Visitors.{0,100}"value">([0-9,]+)/; // or with /g modifier on the end?
myMatches = myRegex.exec(stringToMatch);

myMatches[0] contains the entire matched string. myMatches[1] and higher would contain the first, second, etc. capture groups, denoted by parentheses in the regex. In this case, myMatches[1] contains the number.

Since you don't seem to need to match anything else before the numbers (besides Visitors etc.), I don't think you'll need a lookbehind assertion at all.

Platinum Azure
This regex doesnt match only the numbers but it matches everything from Visitors to the end of the number. I would like to grab only the number though. thanks!!!
That's what the parentheses are for. I'll edit my post to reflect how you'd use the RegExp object to capture the number, sorry for not being clear.
Platinum Azure