开发者

What does this regexp pattern mean?

开发者 https://www.devze.com 2023-02-18 23:40 出处:网络
I\'m working with some Action Script file and I found this: var pattern:RegExp = /.*\\// var results:Array = pattern.exec(cardImageService开发者_C百科.url);

I'm working with some Action Script file and I found this:

var pattern:RegExp = /.*\//  
var results:Array = pattern.exec(cardImageService开发者_C百科.url);

I know it's a regular expression and that exec() is looking for my pattern in my string. But how should I understand this pattern?

Thanks!


/   - Regex delimiter

.   - Meta-character to match any character except newline.
*   - Quantifier for zero or more
\/  - A literal /. Since / is used as a delimiter, to match a literal / we 
      need to escape it.

/   - Regex delimiter


The pattern

.*\/

means

.*      # any character (except \n), zero or more times
\/      # the forward slash "/"

The forward slash must be escaped because when written as a regex literal (like in your case), the forward slash is already in use to delimit the regex.

In other cases, when the regex is presented as a string, it would look like ".*/" and mean the same thing.

Effectively, this matches a path up to (and including) the last forward slash.

/some/very/long/path/with.a.file
-------match---------


It matches any number of characters (zero or more), ending with a / character. Typically, this will grab everything in the string from the beginning until (and including) the last forward slash in the string.


/ is the RegEx delimiter

.* selects 0 or more characters (as many as possible), excluding newlines

\/ is an escape character followed by / so that it will match the / char without ending the regex

/ is the other RegEx delimiter

what this regex will search for is any number of characters followed by a / char.


.* means find anything, any number of times (0 to infinite) and \/ means find a slash, the \ is an escape character. In essence, the regex would match pretty much any line that has a / in it.

0

精彩评论

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