Javascript RegExp对象---获取url中某一个参数的值

RegExp 对象

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

创建 RegExp 对象的语法:

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")

参数

参数 pattern 是一个字符串,指定了正则表达式的模式或其他正则表达式。

参数 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;
}
方法三:
根据传入的参数名称获取到具体的参数值
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事件

        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 (Match match in Regex.Matches(textBox1.Text, "(.*) 表地址:(\\d+) 表数据:(\\d+.\\d)kwh")){}

posted @ 2014-07-18 11:35    阅读(1167)  评论(0编辑  收藏  举报