开发者

How do i use regex in javascript to find three periods inside of a string?

Ok, this is probably a simple regex but i am not familiar with them. I have a bunch of strings like this: '11.9.2.1 Fusion' or '11.9.1 Fusion' etc... All of them have ' Fusion' at the end of them. I am trying to find only the one开发者_开发技巧s with three periods (or four sets of numbers) in them. This is some pseudo code like what i have:

$(xml).find("Asset").each(function(){
    var projectName = $(this).find("Attribute[name='SecurityScope.Name']").text();
    if(projectName.match(where-ive-been-putting-regex-statements)){
        var patch = true;
    }else{
        var patch = false;
    }
});

Any ideas?


Something like this? - /(\d+\.){3}\d+/

It matches 3 groups of numbers (at least 1 digit) followed by a period and a fourth number.


This should do it:

\d+\.\d+\.\d+\.\d+\s+Fusion

Matches 4 groups of any digit (one or more) separated by a literal dot, followed by one or more whitespace, followed by literal string "Fusion"

Matches: 11.9.2.1 Fusion
But does not match: 11.9.1 Fusion

Live example: http://jsfiddle.net/Jqvtp/


This matches strings like '11.9.2.1 Fusion':

projectName.match(/^(\d+\.){3}\d+ Fusion$/)


A regex to match a string that contains three periods would be

.*\..*\..*\..*

(where the first and last .*s may be superfluous depending on whether your regex engine offers a "contains"-style match.)

The critical part here is that . by itself is a character wildcard so will match anything. Escaping it with the backslash turns it into a match for a literal period.

So what we have is (with line breaks for explanation)

.*   // Match any character, any number of times.  Then:
\.   // Match a literal period.  Followed by:
.*   // Any character, any number of times.  Followed by: (etc...)


Try:

[\d]+\.[\d]+\.[\d]+\.[\d]+ Fusion
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜