i want to match a known number(floating point or integer) with a string and finding number of that repeat.
类似“22.5 98 12 22 322”中的22
how i can do it with regular expression ?
i want to match a known number(floating point or integer) with a string and finding number of that repeat.
类似“22.5 98 12 22 322”中的22
how i can do it with regular expression ?
str.match(/substr/g).length
“体”中的“子”发生率。 例如:
str = "22.5 98 12 22 322"
num = str.match(/22/g).length
alert(num) // 3
If you want to match integers rather than just substrings, make sure the number is preceded and followed by a space:
str = "22.5 98 12 22 322"
num = str.match(/(^|s)22($|s)/g).length
alert(num) // 1
另一种选择,没有定期表述:
str = "22.5 98 12 22 322"
num = str.split(" ").filter(function(item) {
return Number(item) === 22;
}).length;
尝试使用:
if (numbers.indexOf("22") != -1) {
//The number 22 is in the String
}
它并非完全是reg,而是为什么在你不需要的时候使用它呢?
如果你只想找到在同一条范围内重复的号码,那么你可以这样做:
var test_str = 22 34 55 45345 34 22 555 ;
var matches = test_str.match(/([0-9]+)(?=.+1)/g);
// matches contains ["22", "34"], but not "55"
var str = "22.5 98 12 22 322";
var pattern = /22/g;
if (pattern.test(str)) {
// it matches "22", do your logic here...
}
http://www.w3schools.com/jsref/jsref_obj_regexp.asp”rel=“nofollow”
Corrolating integer/floating point numbers from text to regex is
very tricky, since regex has no such concept.
With dynamic input, all the work is constructing the regex programatically.
This example doesn t include scientific notation, but it could.
Pseudo Code:
If the input matches this parse regex
/^ (?=.*d) // must be a digit in the input
([+-]?) // capt (1), optional +/-
[0]* // suck up all leading 0 s
(d*) // capt (2), optional digits, will start with [1-9]
([.]?) // capt (3), optional period
(d*?) // capt (4), non-greedy optional digits, will start with [1-9]
[0]* // suck up all trailing 0 s
$/x
sign = [+]? ;
If !length $2 AND !length $4
sign = [+-] ;
Else
If $1 == -
sign = [-] ;
Endif
Endif
whole = [0]* ;
If length $2
whole = [0]* + $2;
Endif
decfrac = [.]? [0]* ;
If length $4
$decfrac = [.] + $4 + [0]* ;
Endif
regex = /(?:^|s)(?=S*d) + "$sign $whole $decfrac" + (?:s|$)/x ;
Else
// input is not valid
Endif
This is a Perl test case.
The dynamic regex produced has not been tested,
I asume it works but I could be wrong.
@samps = (
00000.0 ,
+.0 ,
-.0 ,
0. ,
-0.00100 ,
1 ,
+.1 ,
+43.0910 ,
22 ,
33.e19 ,
);
for (@samps)
{
print "
Input: $_
";
if( /^ (?=.*d) ([+-]?) [0]*(d*) ([.]?) (d*?)[0]*$/x ) # 1,2,3,4
{
my ($sign, $whole, $decfrac);
$sign = [+]? ;
if ( !length $2 && !length $4) {
$sign = [+-] ;
} elsif ($1 eq - ) {
$sign = [-] ;
}
$whole = [0]* ;
if ( length $2) {
$whole = [0]* .$2;
}
$decfrac = [.]? [0]* ;
if ( length $4) {
$decfrac = [.] .$4. [0]* ;
}
my $regex = /(?:^|s)(?=S*d) . "$sign $whole $decfrac" . (?:s|$)/x ;
print "Regex: $regex
";
}
else {
print "**input $_ is not valid
";
next;
}
}
产出
Input: 00000.0
Regex: /(?:^|s)(?=S*d) [+-] [0]* [.]? [0]* (?:s|$)/x
Input: +.0
Regex: /(?:^|s)(?=S*d) [+-] [0]* [.]? [0]* (?:s|$)/x
Input: -.0
Regex: /(?:^|s)(?=S*d) [+-] [0]* [.]? [0]* (?:s|$)/x
Input: 0.
Regex: /(?:^|s)(?=S*d) [+-] [0]* [.]? [0]* (?:s|$)/x
Input: -0.00100
Regex: /(?:^|s)(?=S*d) [-] [0]* [.] 001[0]* (?:s|$)/x
Input: 1
Regex: /(?:^|s)(?=S*d) [+]? [0]*1 [.]? [0]* (?:s|$)/x
Input: +.1
Regex: /(?:^|s)(?=S*d) [+]? [0]* [.] 1[0]* (?:s|$)/x
Input: +43.0910
Regex: /(?:^|s)(?=S*d) [+]? [0]*43 [.] 091[0]* (?:s|$)/x
Input: 22
Regex: /(?:^|s)(?=S*d) [+]? [0]*22 [.]? [0]* (?:s|$)/x
Input: 33.e19
**input 33.e19 is not valid
Here s a good tool for you to explore regular expressions :) http://www.regular-expressions.info/javascriptexample.html
从上述网站通过:
function demoShowMatchClick() {
var re = new RegExp("(?:^| )+(22(?:$| ))+");
var m = re.exec("22.5 98 12 22 322");
if (m == null) {
alert("No match");
} else {
var s = "Match at position " + m.index + ":
";
for (i = 0; i < m.length; i++) {
s = s + m[i] + "
";
}
alert(s);
}
}
这将使你们都能够相匹配,并且要求通过铺.或白色空间的开端或尾端打消数量。
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.