RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具。
/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是正则表达式,而不是字符串,则必须省略该参数。
一个新的 RegExp 对象,具有指定的模式和标志。如果参数pattern是正则表达式而不是字符串,那么 RegExp() 构造函数将用与指定的 RegExp 相同的模式和标志创建一个新的 RegExp 对象。
如果不用 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;}方法三:根据传入的参数名称获取到具体的参数值
functiongetUrlParam(name){varreg=newRegExp("(^|&)"+name+"=([^&]*)(&|$)");//构造一个含有目标参数的正则表达式对象varr=window.location.search.substr(1).match(reg);//匹配目标参数if(r!=null)returnunescape(r[2]);returnnull;//返回参数值}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事件
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; }
替换输入的内容所有匹配的文字
javascript:
<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>
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(MatchmatchinRegex.Matches(textBox1.Text,"(.*)表地址:(//d+)表数据:(//d+.//d)kwh")){}
新闻热点
疑难解答