JavaScript 不支持正向回顾
我在 .Net 中有以下正则表达式
(?<=Visitors.{0,100}?"value">)[0-9,]+(?=</div>)
,并在正则表达式
<div class="text">Visitors</div> <div class="value">14,000,000</div>
<div class="text">VisitorsFromItaly</div> <div class="value">15,200</div>
中指定了以下文本“Visitors”或“VisitorsFromItaly”,我得到了特定数量的访客数量。
我想在 javascript 中执行此操作,但不使用任何 .replace 方法。 我只需要一个简单的 javascript 正则表达式。 我是 javascript 正则表达式的新手,所以我不知道如何转换它。 它说 javascript 不支持lookbehind,这就是我的 .Net 正则表达式工作的原因。
任何帮助,将不胜感激。 非常感谢!
I have the following regular expression in .Net
(?<=Visitors.{0,100}?"value">)[0-9,]+(?=</div>)
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!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解您的意思...您真的需要后向断言吗?
你不能只匹配下面这样的东西吗?
myMatches[0] 包含整个匹配的字符串。 myMatches[1] 及更高版本将包含第一个、第二个等捕获组,在正则表达式中用括号表示。 在本例中,myMatches[1] 包含该数字。
由于您似乎不需要在数字之前匹配任何其他内容(除了访问者等),因此我认为您根本不需要向后断言。
I'm not sure I understand you... Do you really need a lookbehind assertion?
Can't you just match something like the following?
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.
尝试一下:
Try it:
您是否以字符串形式获取此数据? 或者您是否有权访问 DOM 对象(例如 AJAX 响应)?
如果您可以访问后者,我就不会为正则表达式烦恼。
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.