RegExp 對象表示正則表達式,它是對字符串執行模式匹配的強大工具。javascript
/pattern/attributes
實例:window.location.href:http://localhost:8100/aspx/main/ServiceCenter_list.aspx?category_id=93&page=2
要匹配到的category_id=93:/category_id=\d+/g
new RegExp(pattern, attributes);
實例:window.location.href:http://localhost:8100/aspx/main/ServiceCenter_list.aspx?category_id=93&page=2
要匹配到的category_id=93:new RegExp("category_id=\\d+","g")
參數 attributes 是一個可選的字符串,包含屬性 "g"、"i" 和 "m",分別用於指定全局匹配、區分大小寫的匹配和多行匹配。ECMAScript 標準化以前,不支持 m 屬性。若是 pattern 是正則表達式,而不是字符串,則必須省略該參數。java
一個新的 RegExp 對象,具備指定的模式和標誌。若是參數 pattern 是正則表達式而不是字符串,那麼 RegExp() 構造函數將用與指定的 RegExp 相同的模式和標誌建立一個新的 RegExp 對象。web
若是不用 new 運算符,而將 RegExp() 做爲函數調用,那麼它的行爲與用 new 運算符調用時同樣,只是當 pattern 是正則表達式時,它只返回 pattern,而再也不建立一個新的 RegExp 對象。正則表達式
1.實例:使用javascript語言用正則獲取url中某一個參數函數
地址:window.location.href:http://localhost:8100/aspx/main/ServiceCenter_list.aspx?category_id=93&page=2
目的:獲取到93參數值
方法一:
function GetCate(){
var str= window.location.href;
var CateID=str.match(/category_id=(\d)+/i)[1];
return CateID;
}
方法二:
function GetCate(){
var str=window.location.href;
var CateID=str.match(new RegExp("category_id=\\d+","i")).replace("category_id","");
return CateID;
}
方法三:
根據傳入的參數名稱獲取到具體的參數值
function getUrlParam(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //構造一個含有目標參數的正則表達式對象 var r = window.location.search.substr(1).match(reg); //匹配目標參數 if (r!=null) return unescape(r[2]); return null; //返回參數值 }
var CateID=getUrlParam('category_id')
----------------------------------------------------------追加.net中Regex使用實例-------------------------------------------------------------------------------------
實例一
public string FormatQuery(string str) { string result = string.Empty; Regex rgx = new Regex("(\\s+)"); Match m = rgx.Match(str); if (m.Success) { return str.Replace(m.Groups[1].ToString(), "%"); } else { return str; } }
實例二
public static string GetContent(string strContent)
{
int i = 0;
Regex rgx = new Regex("src=\"(/uploads/image/)");
if (rgx.IsMatch(strContent))
{
foreach (Match m in Regex.Matches(strContent, "src=\"(/uploads/image/)"))
{
if (i==0)
{
strContent = strContent.Replace(m.Groups[1].ToString(), new ReadSiteConfig().LoadConfig().MJwebsite + m.Groups[1].ToString());
}
i += 1;
}
return strContent;
}
else
{
return strContent;
}
}工具
實例三 修改內容(文字和圖文)中全部圖片的地址並添加onerror事件this
public string ContentImages(string content) { string formatedcontent = string.Empty; Regex rgx = new Regex("<img(\\s)?src=\"([/A-Za-z0-9_.jpg]+)\"(\\s)?alt=\"\" />",RegexOptions.IgnoreCase); if (rgx.IsMatch(content)) { int i = 0; foreach (Match m in Regex.Matches(content, "<img(\\s)?src=\"([/A-Za-z0-9_.jpg]+)\"(\\s)?alt=\"\" />")) { if (i==0) { content = content.Replace(m.Groups[2].ToString(), "http://www.baidu.com" + m.Groups[2]); } i += 1; } int j = 0; foreach (Match match in Regex.Matches(content, "<(img)(\\s)?src")) { if (j == 0) { content = content.Replace(match.Groups[1].ToString(), match.Groups[1] + " onerror = \"this.src='/images/noimg.gif'\""); } j += 1; } return content; } return content; }
替換輸入的內容全部匹配的文字url
javascript:spa
<script type="text/javascript">
var s = '123<p style="border:1px solid red">333</p>123';
var r = /style="[^"]*"/g;
alert(s.replace(r, ''));
</script>.net
C#方法
public static void Main() { string input = "This is text with far too much " + "whitespace."; string pattern = "\\s+"; string replacement = " "; Regex rgx = new Regex(pattern); string result = rgx.Replace(input, replacement); Console.WriteLine("Original String: {0}", input); Console.WriteLine("Replacement String: {0}", result); }
//循環遍歷匹配到的值
foreach (Match match in Regex.Matches(textBox1.Text, "(.*) 表地址:(\\d+) 表數據:(\\d+.\\d)kwh")){}