RegEx 」标签的归档

Regular Expressions in JavaScript

1.定義

Javascript中,定義正則表達式的方法有兩個:

1) RegExp Literal

/* /pattern/flags; */
var re = /mac/i;

2) RegExp Object Constructor

/* new RegExp("pattern","flags"); */
var re = new RegExp(window.prompt("Please input a regex.","yes|yeah"),"g");

 

2. Flag

1) Global Search

g     The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.

2) Flags

i     The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as ?, ü, ?, ?.

3) Multiline Input

m     This flag makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively.

 

3. Pattern

參閱此處

 

4. 應用

1) RegExp.exec(string)

Applies the RegExp to the given string, and returns the match information.

var match = /s(amp)le/i.exec("Sample text")
//match then contains ["Sample","amp"] 

2) RegExp.test(string)

Tests if the given string matches the Regexp, and returns true if matching, false if not.   

var match = /sample/.test("Sample text")
//match then contains false 

3) String.match(pattern)

Matches given string with the RegExp. With g flag returns an array containing the matches, without g flag returns just the first match or if no match is found returns null.   

var str = "Watch out for the rock!".match(/r?or?/g)
//str then contains ["o","or","ro"] 

4) String.search(pattern)

Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not.   

var ndx = "Watch out for the rock!".search(/for/)
//ndx then contains 10 

5) String.replace(pattern,string)

Replaces matches with the given string, and returns the edited string.   

var str = "Liorean said: My name is Liorean!".replace(/Liorean/g,'Big Fat Dork')
//str then contains "Big Fat Dork said: My name is Big Fat Dork!" 

6) String.split(pattern)

Cuts a string into an array, making cuts at matches.   

var str = "I am confused".split(/\s/g)
//str then contains ["I","am","confused"]

 

Example:

var s='<div style="text-align:center;width:inherit;text-color:blue;">SOME TEXT</div>'
var rx=new RegExp("<div .*?>(.*?)</div>","i");
s=s.replace(rx,"$1");

// s="SOME TEXT"
// http://www.tek-tips.com/viewthread.cfm?qid=1231301

article clipper remember Regular Expressions in JavaScript
 

Using Regular Expression to get string

文議論了如果通過正則表達式來處理string, 一直想寫,但是礙於時間問題,無法執筆,下個月吧,寫一篇總結性的文章,現在暫且看看別人的成果 Reference.


在进行网页内容下载时,经常需要截取指定关键字内的部分文本,以前一般使用Instr、InstrRev、Mid、Left和Right这些字符串函数来分离,这样比较繁琐一些。其实也可以使用正则表达式来处理,这里做一个自定义函数,以后自己就可以这样使用了。

Function TextBetween(strInput As String, _
                                   strStart As String, _
                                   strEnd As String) As String
    Dim regEx As Object
    Dim Matches As Object
    Dim strPattern As String

    Set regEx = CreateObject("vbscript.regexp")

    strStart = Replace(strStart, "\", "\\")
    strEnd = Replace(strEnd, "\", "\\")

    strStart = Replace(strStart, "(", "\(")
    strEnd = Replace(strEnd, "(", "\(")

    strStart = Replace(strStart, ")", "\)")
    strEnd = Replace(strEnd, ")", "\)")

    strStart = Replace(strStart, "*", "\*")
    strEnd = Replace(strEnd, "*", "\*")

    strStart = Replace(strStart, ".", "\.")
    strEnd = Replace(strEnd, ".", "\.")

    strStart = Replace(strStart, "^", "\^")
    strEnd = Replace(strEnd, "^", "\^")

    strStart = Replace(strStart, "$", "\$")
    strEnd = Replace(strEnd, "$", "\$")

    strStart = Replace(strStart, "$", "\$")
    strEnd = Replace(strEnd, "$", "\$")

    strStart = Replace(strStart, "+", "\+")
    strEnd = Replace(strEnd, "+", "\+")

    strStart = Replace(strStart, "?", "\?")
    strEnd = Replace(strEnd, "?", "\?")

    strStart = Replace(strStart, "{", "\{")
    strEnd = Replace(strEnd, "{", "\{")

    strStart = Replace(strStart, "}", "\}")
    strEnd = Replace(strEnd, "}", "\}")

    strStart = Replace(strStart, "|", "\|")
    strEnd = Replace(strEnd, "|", "\|")

    strPattern = strStart &amp;amp; "(.*\n*.*)" &amp;amp; strEnd
    regEx.Pattern = strPattern
    regEx.IgnoreCase = True
    regEx.Global = True
    regEx.MultiLine = True

    On Error Resume Next
    Set Matches = regEx.Execute(strInput)
    TextBetween = Matches(0).submatches(0)
    Set regEx = Nothing
    Set Matches = Nothing
End Function

Sub Test()
    MsgBox TextBetween("abc-this is a test-abc", "-", "-")
End Sub
article clipper remember Using Regular Expression to get string