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

javascript - Matching multiple input values in regex - Stack Overflow

programmeradmin0浏览0评论

I have the following input values and I was hoping someone could kindly tell me the best way to match all of the "values" (217, 218, 219) -- The html is escaped and that's what I need to match.

<input type=\"hidden\" name=\"id\" value=\"217\"\/>

<input type=\"hidden\" name=\"id\" value=\"218\"\/>

<input type=\"hidden\" name=\"id\" value=\"219\"\/>

I have the following input values and I was hoping someone could kindly tell me the best way to match all of the "values" (217, 218, 219) -- The html is escaped and that's what I need to match.

<input type=\"hidden\" name=\"id\" value=\"217\"\/>

<input type=\"hidden\" name=\"id\" value=\"218\"\/>

<input type=\"hidden\" name=\"id\" value=\"219\"\/>
Share Improve this question edited Sep 4, 2010 at 1:23 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Sep 3, 2010 at 17:11 RyanLynchRyanLynch 3,0074 gold badges38 silver badges48 bronze badges 3
  • Or which IDE? [stupid ment min length limit!] – Peter Boughton Commented Sep 3, 2010 at 17:24
  • Are all the lines in a single string ? Or are they read from a file ? – Toto Commented Sep 3, 2010 at 17:34
  • Javascript, it's for a jMeter test.. Read from a JSON response.. so single line – RyanLynch Commented Sep 3, 2010 at 17:37
Add a ment  | 

2 Answers 2

Reset to default 4

Based on your ments to other responses, you actually only want to match on the numbers if they are fit the pattern name=\"id\" value=\"###\" and thus there are four possibilities, depending on how precise you want your matches to be. Also, based on your ments, I'm using javascript as the language of implementation.

Also, note that a previous answer incorrectly escaped the slashes around the id and value strings.

FWIW, I have tested each of the options below:

Option 1: Match any number

//build the pattern
var pattern = /name=\"id\" value=\"([0-9]+)\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

Option 2: Match any 3-digit number

//build the pattern
var pattern = /name=\"id\" value=\"([0-9]{3})\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

Option 3: Match specific 3-digit number ranges

//build the pattern; modify to fit your ranges
//  This example matches 110-159 and 210-259
var pattern = /name=\"id\" value=\"([1-2][1-5][0-9])\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

Option 4: Match specific 3-digit numbers

//build the pattern; modify to fit your numbers
//  This example matches 217, 218, 219 and 253
var pattern = /name=\"id\" value=\"(217|218|219|253)\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

I don'ty know in what language, but supposing the code you showed is a string (since you escaped all quotes) you can pass that string into the following regexp that is going to match any sequence of numbers.

/([0-9]+)/g

Based on the language you use you need to port this regexp and use the proper function.

In JS u can use:

var array_matches = "your string".match(/([0-9]+)/g);

In PHP u can use:

preg_match("([0-9]+)", "your string", array_matches);
发布评论

评论列表(0)

  1. 暂无评论