最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - RegEx in NodeJavascript - how to get pattern match bounds? - Stack Overflow

programmeradmin4浏览0评论

Currently, I'm using regex in Javascript / Node via find() and that works for finding the beginning of the pattern. But I'd also like to be able to find out where the pattern ends. Is that possible?

Currently, I'm using regex in Javascript / Node via find() and that works for finding the beginning of the pattern. But I'd also like to be able to find out where the pattern ends. Is that possible?

Share Improve this question asked Jun 26, 2012 at 20:09 ControlAltDelControlAltDel 35.1k10 gold badges58 silver badges83 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

If you use the RegExp.exec() method, you can get the information you need.

var pattern = /\d+\.?\d*|\.\d+/;
var match = pattern.exec("the number is 7.5!");

var start = match.index;
var text = match[0];
var end = start + text.length;

/\d+\.?\d*|\.\d+/ is equivalent to new RegExp("\\d+\\.?|\\.\\d+"). The literal syntax saves some backslashes.

发布评论

评论列表(0)

  1. 暂无评论