admin管理员组文章数量:1026989
I'm having real difficulty with this but I'm no javascript expert. All I want to do is get myself an array of all matches in a string which match a given regExp. The regExp being this :
[0-9]+
ie. Any integer.
So If I pass the string "12 09:8:76:::54 12" I should get
arr[0]="12" arr[1]="09" arr[2]="8" arr[3]="76" arr[4]="54" arr[5]="12"
Easy? Not for me! I could do this in vb no problem with regexp.matches(string) (something like that anyway). I thought that the javascript method .exec would also give me an array however it only returns the first match. What's going on? Code...
function testIt(){
splitOutSelection2("123:::45 0::12312 12:17");
}
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = regExp.exec(sSelection);
};
I'm having real difficulty with this but I'm no javascript expert. All I want to do is get myself an array of all matches in a string which match a given regExp. The regExp being this :
[0-9]+
ie. Any integer.
So If I pass the string "12 09:8:76:::54 12" I should get
arr[0]="12" arr[1]="09" arr[2]="8" arr[3]="76" arr[4]="54" arr[5]="12"
Easy? Not for me! I could do this in vb no problem with regexp.matches(string) (something like that anyway). I thought that the javascript method .exec would also give me an array however it only returns the first match. What's going on? Code...
function testIt(){
splitOutSelection2("123:::45 0::12312 12:17");
}
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = regExp.exec(sSelection);
};
Share
Improve this question
edited Aug 11, 2010 at 17:02
El Ronnoco
asked Jul 19, 2010 at 8:45
El RonnocoEl Ronnoco
11.9k5 gold badges40 silver badges67 bronze badges
5 Answers
Reset to default 3arr = sSelection.match(/[0-9]+/g);
should do.
g
is the global modifier that you need to get all the matches, not just the first one.
something like:
var arrMatch = "12 09:8:76:::54 12".match(/[0-9]+/g);
alert(arrMatch);
.match will return an array if global is set (and matches are found of course). [0-9]+ means it will search for not only single digits, but also match 12, 09, 76.
According to the doc, exec return the first match. You should use match instead.
var arr = sSelection.match(/[0-9]+/g);
or
var arr = sSelection.match(/\d+/g);
All the answers work but I was wanting to keep my regExp object rather than specify it at the time of use. So simply changing the function to...
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = sSelection.match(regExp);
};
..is what I was looking for. Thanks for pointing me in the right direction though to all who have replied.
function splitOutSelection2(sSelection){
return sSelection.split(/[^0-9]+/g);
};
Negate the regExp and use String#split.
I'm having real difficulty with this but I'm no javascript expert. All I want to do is get myself an array of all matches in a string which match a given regExp. The regExp being this :
[0-9]+
ie. Any integer.
So If I pass the string "12 09:8:76:::54 12" I should get
arr[0]="12" arr[1]="09" arr[2]="8" arr[3]="76" arr[4]="54" arr[5]="12"
Easy? Not for me! I could do this in vb no problem with regexp.matches(string) (something like that anyway). I thought that the javascript method .exec would also give me an array however it only returns the first match. What's going on? Code...
function testIt(){
splitOutSelection2("123:::45 0::12312 12:17");
}
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = regExp.exec(sSelection);
};
I'm having real difficulty with this but I'm no javascript expert. All I want to do is get myself an array of all matches in a string which match a given regExp. The regExp being this :
[0-9]+
ie. Any integer.
So If I pass the string "12 09:8:76:::54 12" I should get
arr[0]="12" arr[1]="09" arr[2]="8" arr[3]="76" arr[4]="54" arr[5]="12"
Easy? Not for me! I could do this in vb no problem with regexp.matches(string) (something like that anyway). I thought that the javascript method .exec would also give me an array however it only returns the first match. What's going on? Code...
function testIt(){
splitOutSelection2("123:::45 0::12312 12:17");
}
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = regExp.exec(sSelection);
};
Share
Improve this question
edited Aug 11, 2010 at 17:02
El Ronnoco
asked Jul 19, 2010 at 8:45
El RonnocoEl Ronnoco
11.9k5 gold badges40 silver badges67 bronze badges
5 Answers
Reset to default 3arr = sSelection.match(/[0-9]+/g);
should do.
g
is the global modifier that you need to get all the matches, not just the first one.
something like:
var arrMatch = "12 09:8:76:::54 12".match(/[0-9]+/g);
alert(arrMatch);
.match will return an array if global is set (and matches are found of course). [0-9]+ means it will search for not only single digits, but also match 12, 09, 76.
According to the doc, exec return the first match. You should use match instead.
var arr = sSelection.match(/[0-9]+/g);
or
var arr = sSelection.match(/\d+/g);
All the answers work but I was wanting to keep my regExp object rather than specify it at the time of use. So simply changing the function to...
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = sSelection.match(regExp);
};
..is what I was looking for. Thanks for pointing me in the right direction though to all who have replied.
function splitOutSelection2(sSelection){
return sSelection.split(/[^0-9]+/g);
};
Negate the regExp and use String#split.
本文标签: regexExtracting an array of integers using a regular expression in javascriptStack Overflow
版权声明:本文标题:regex - Extracting an array of integers using a regular expression in javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745633222a2160287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论