第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求大神,幫忙解釋下面段正則表達式的含義

求大神,幫忙解釋下面段正則表達式的含義

縹緲止盈 2022-02-19 15:11:46
這應(yīng)該是判斷郵件的 最好逐個解釋下^(\w+((-\w+)|(\.\w+))*)\+\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$
查看完整描述

2 回答

?
弒天下

TA貢獻1818條經(jīng)驗 獲得超8個贊

匹配中文字符的正則表達式: [\u4e00-\u9fa5]

匹配雙字節(jié)字符(包括漢字在內(nèi)):[^\x00-\xff]

匹配空行的正則表達式:\n[\s| ]*\r

匹配HTML標(biāo)記的正則表達式:/<(.*)>.*<\/\1>|<(.*) \/>/

匹配首尾空格的正則表達式:(^\s*)|(\s*$)

匹配Email地址的正則表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配網(wǎng)址URL的正則表達式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

(1)應(yīng)用:計算字符串的長度(一個雙字節(jié)字符長度計2,ASCII字符計1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

(2)應(yīng)用:javascript中沒有像vbscript那樣的trim函數(shù),我們就可以利用這個表達式來實現(xiàn)
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
(3)應(yīng)用:利用正則表達式分解和轉(zhuǎn)換IP地址
function IP2V(ip) //IP地址轉(zhuǎn)換成對應(yīng)數(shù)值
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正則表達式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}

(4)應(yīng)用:從URL地址中提取文件名的javascript程序

s="http://www.9499.net/page1.htm";
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2") ; //Page1.htm

(5)應(yīng)用:利用正則表達式限制網(wǎng)頁表單里的文本框輸入內(nèi)容

用正則表達式限制只能輸入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
用正則表達式限制只能輸入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
用正則表達式限制只能輸入數(shù)字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
用正則表達式限制只能輸入數(shù)字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

====================================================
正則表達式大全

正則表達式用于字符串處理,表單驗證等場合,實用高效,但用到時總是不太把握,以致往往要上網(wǎng)查一番。我將一些常用的表達式收藏在這里,作備忘之用。本貼隨時會更新。

匹配中文字符的正則表達式: [\u4e00-\u9fa5]

匹配雙字節(jié)字符(包括漢字在內(nèi)):[^\x00-\xff]

應(yīng)用:計算字符串的長度(一個雙字節(jié)字符長度計2,ASCII字符計1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

匹配空行的正則表達式:\n[\s| ]*\r

匹配HTML標(biāo)記的正則表達式:/<(.*)>.*<\/\1>|<(.*) \/>/

匹配首尾空格的正則表達式:(^\s*)|(\s*$)

應(yīng)用:javascript中沒有像vbscript那樣的trim函數(shù),我們就可以利用這個表達式來實現(xiàn),如下:

String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

利用正則表達式分解和轉(zhuǎn)換IP地址:

下面是利用正則表達式匹配IP地址,并將IP地址轉(zhuǎn)換成對應(yīng)數(shù)值的javascript程序:

function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正則表達式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}

不過上面的程序如果不用正則表達式,而直接用split函數(shù)來分解可能更簡單,程序如下:

var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))

匹配Email地址的正則表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配網(wǎng)址URL的正則表達式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

利用正則表達式去除字串中重復(fù)的字符的算法程序:[注:此程序不正確,原因見本貼回復(fù)]

var s="abacabefgeeii"
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g"?瓊?渦獢p?????浜睹扥潛桴牥摜極慢?瑨m?)
var s2=s.replace(re,"")
alert(s1+s2) //結(jié)果為:abcefgi

我原來在CSDN上發(fā)貼尋求一個表達式來實現(xiàn)去除重復(fù)字符的方法,最終沒有找到,這是我能想到的最簡單的實現(xiàn)方法。思路是使用后向引用取出包括重復(fù)的字符,再以重復(fù)的字符建立第二個表達式,取到不重復(fù)的字符,兩者串連。這個方法對于字符順序有要求的字符串可能不適用。

得用正則表達式從URL地址中提取文件名的javascript程序,如下結(jié)果為page1

s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")
alert(s)

利用正則表達式限制網(wǎng)頁表單里的文本框輸入內(nèi)容:

用正則表達式限制只能輸入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"

用正則表達式限制只能輸入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用正則表達式限制只能輸入數(shù)字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用正則表達式限制只能輸入數(shù)字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

補充:
^\d+$ //匹配非負(fù)整數(shù)(正整數(shù) + 0)
^[0-9]*[1-9][0-9]*$ //匹配正整數(shù)
^((-\d+)|(0+))$ //匹配非正整數(shù)(負(fù)整數(shù) + 0)
^-[0-9]*[1-9][0-9]*$ //匹配負(fù)整數(shù)
^-?\d+$ //匹配整數(shù)
^\d+(\.\d+)?$ //匹配非負(fù)浮點數(shù)(正浮點數(shù) + 0)
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮點數(shù)
^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //匹配非正浮點數(shù)(負(fù)浮點數(shù) + 0)
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配負(fù)浮點數(shù)
^(-?\d+)(\.\d+)?$ //匹配浮點數(shù)
^[A-Za-z]+$ //匹配由26個英文字母組成的字符串
^[A-Z]+$ //匹配由26個英文字母的大寫組成的字符串
^[a-z]+$ //匹配由26個英文字母的小寫組成的字符串
^[A-Za-z0-9]+$ //匹配由數(shù)字和26個英文字母組成的字符串
^\w+$ //匹配由數(shù)字、26個英文字母或者下劃線組成的字符串
^[\w-]+(\.[\w?瓊?渦獢p?????浜睹扥潛桴牥摜極慢?瑨m?-]+)*@[\w-]+(\.[\w-]+)+$ //匹配email地址
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ //匹配url

利用正則表達式去除字串中重復(fù)的字符的算法程序:

var s="abacabefgeeii"
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //結(jié)果為:abcefgi
===============================
如果var s = "abacabefggeeii"
結(jié)果就不對了,結(jié)果為:abeicfgg
正則表達式的能力有限

RE: totoro
謝謝你的指點,這個javascript正則表達式程序算法確實有問題,我會試著找更好的辦法!!!

1.確認(rèn)有效電子郵件格式
下面的代碼示例使用靜態(tài) Regex.IsMatch 方法驗證一個字符串是否為有效電子郵件格式。如果字符串包含一個有效的電子郵件地址,則 IsValidEmail 方法返回 true,否則返回 false,但不采取其他任何操作。您可以使用 IsValidEmail,在應(yīng)用程序?qū)⒌刂反鎯υ跀?shù)據(jù)庫中或顯示在 ASP.NET 頁中之前,篩選出包含無效字符的電子郵件地址。

[Visual Basic]
Function IsValidEmail(strIn As String) As Boolean
' Return true if strIn is in valid e-mail format.
Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
End Function
[C#]
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}

2.清理輸入字符串
下面的代碼示例使用靜態(tài) Regex.Replace 方法從字符串中抽出無效字符。您可以使用這里定義的 CleanInput 方法,清除掉在接受用戶輸入的窗體的文本字段中輸入的可能有害的字符。CleanInput 在清除掉除 @、-(連字符)和 .(句點)以外的所有非字母數(shù)字字符后返回一個字符串。

[Visual Basic]
Function CleanInput(strIn As String) As String
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function
[C#]
String CleanInput(string strIn)
?瓊?渦獢p?????浜睹扥潛桴牥摜極慢?瑨m?{
// Replace invalid characters with empty strings.
return Regex.Replace(strIn, @"[^\w\.@-]", "");
}

3.更改日期格式
以下代碼示例使用 Regex.Replace 方法來用 dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。

[Visual Basic]
Function MDYToDMY(input As String) As String
Return Regex.Replace(input, _
"\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b", _
"${day}-${month}-${year}")
End Function
[C#]
String MDYToDMY(String input)
{
return Regex.Replace(input,
"\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b",
"${day}-${month}-${year}");
}
Regex 替換模式
本示例說明如何在 Regex.Replace 的替換模式中使用命名的反向引用。其中,替換表達式 ${day} 插入由 (?<day>...) 組捕獲的子字符串。

有幾種靜態(tài)函數(shù)使您可以在使用正則表達式操作時無需創(chuàng)建顯式正則表達式對象,而 Regex.Replace 函數(shù)正是其中之一。如果您不想保留編譯的正則表達式,這將給您帶來方便

4.提取 URL 信息
以下代碼示例使用 Match.Result 來從 URL 提取協(xié)議和端口號。例如,“http://www.contoso.com:8080/letters/readme.html”將返回“http:8080”。

[Visual Basic]
Function Extension(url As String) As String
Dim r As New Regex("^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", _
RegexOptions.Compiled)
Return r.Match(url).Result("${proto}${port}")
End Function
[C#]
String Extension(String url)
{
Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
RegexOptions.Compiled);
return r.Match(url).Result("${proto}${port}");
}

今天有網(wǎng)友問:如何用正則表達式表示要么是數(shù)字要么是字母 是字母的話只能是一個字母 數(shù)字則無所謂?
我的回答是:
^[a-zA-Z]$|^\d+$



查看完整回答
反對 回復(fù) 2022-02-21
?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

^(\w+((-\w+)|(\.\w+))*)\+\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$

Assert position at the beginning of the string ?^?
Match the regular expression below and capture its match into backreference number 1 ?(\w+((-\w+)|(\.\w+))*)?
Match a single character that is a “word character” (letters, digits, etc.) ?\w+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Match the regular expression below and capture its match into backreference number 2 ?((-\w+)|(\.\w+))*?
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ?*?
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. ?*?
Match either the regular expression below (attempting the next alternative only if this one fails) ?(-\w+)?
Match the regular expression below and capture its match into backreference number 3 ?(-\w+)?
Match the character “-” literally ?-?
Match a single character that is a “word character” (letters, digits, etc.) ?\w+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Or match regular expression number 2 below (the entire group fails if this one fails to match) ?(\.\w+)?
Match the regular expression below and capture its match into backreference number 4 ?(\.\w+)?
Match the character “.” literally ?\.?
Match a single character that is a “word character” (letters, digits, etc.) ?\w+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Match the character “+” literally ?\+?
Match a single character that is a “word character” (letters, digits, etc.) ?\w+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Match the regular expression below and capture its match into backreference number 5 ?((-\w+)|(\.\w+))*?
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ?*?
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. ?*?
Match either the regular expression below (attempting the next alternative only if this one fails) ?(-\w+)?
Match the regular expression below and capture its match into backreference number 6 ?(-\w+)?
Match the character “-” literally ?-?
Match a single character that is a “word character” (letters, digits, etc.) ?\w+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Or match regular expression number 2 below (the entire group fails if this one fails to match) ?(\.\w+)?
Match the regular expression below and capture its match into backreference number 7 ?(\.\w+)?
Match the character “.” literally ?\.?
Match a single character that is a “word character” (letters, digits, etc.) ?\w+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Match the character “@” literally ?\@?
Match a single character present in the list below ?[A-Za-z0-9]+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
A character in the range between “A” and “Z” ?A-Z?
A character in the range between “a” and “z” ?a-z?
A character in the range between “0” and “9” ?0-9?
Match the regular expression below and capture its match into backreference number 8 ?((\.|-)[A-Za-z0-9]+)*?
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ?*?
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. ?*?
Match the regular expression below and capture its match into backreference number 9 ?(\.|-)?
Match either the regular expression below (attempting the next alternative only if this one fails) ?\.?
Match the character “.” literally ?\.?
Or match regular expression number 2 below (the entire group fails if this one fails to match) ?-?
Match the character “-” literally ?-?
Match a single character present in the list below ?[A-Za-z0-9]+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
A character in the range between “A” and “Z” ?A-Z?
A character in the range between “a” and “z” ?a-z?
A character in the range between “0” and “9” ?0-9?
Match the character “.” literally ?\.?
Match a single character present in the list below ?[A-Za-z0-9]+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
A character in the range between “A” and “Z” ?A-Z?
A character in the range between “a” and “z” ?a-z?
A character in the range between “0” and “9” ?0-9?
Assert position at the end of the string (or before the line break at the end of the string, if any) ?$?



查看完整回答
反對 回復(fù) 2022-02-21
  • 2 回答
  • 0 關(guān)注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號