<link rel="stylesheet" type="text/css" href="../css/orber.css">
<link rel="shortcut icon" href="../index/index/photo/icon.ico">我想用正则表达式将上面的连接改成绝对路径 假设当前路径是 http://www.123.com/text/
我写了条表达式用于取得连接 ../css/orber.css 再进行替换入口string 
<link rel="stylesheet" type="text/css" href='../css/orber.css'>
<link rel="shortcut icon" href="../index/index/photo/icon.ico">表达式:
(?<=(?:src|href|url|background)\s*=\s*["']?)(?=[\s"']?)(?!(?:http://|/))([^\s"'>]*)
替换:
http://www.123.com/text/用System.Text.RegularExpressions.Regex.Replace()方法
但得到的是
<link rel="stylesheet" type="text/css" href=http://www.123.com/text/'http://www.123.com/text/../css/orber.css'>
<link rel="shortcut icon" href=http://www.123.com/text/"http://www.123.com/text/../index/index/photo/icon.ico">请帮忙看看是什么问题

解决方案 »

  1.   

    ResultString = Regex.Replace(yourStr, "href=\"[^/]+([^\"]+)\"", "href=\"http://www.123.com/text$1\"");
      

  2.   

    或者直接用replace("..","http://www.123.com/text/")无需正则
      

  3.   

    用不用singleline都一样
    如果这么简单就不用来这问了
      

  4.   

    或者直接用replace("..","http://www.123.com/text/")无需正则上面只是情况之一 如果是
    <link rel="stylesheet" type="text/css" href="css/orber.css">
    <link rel="shortcut icon" href="index/index/photo/icon.ico">
    这样你怎么处理还有你这个只是对 href=XXXX 进行处理 如果是 src=XXXX  url=XXXX background=XXX
    如果 href = XXX(带空格) 或 href = "XXX" 或 href = 'XXX' 你怎么处理
    ResultString = Regex.Replace(yourStr, "href=\"[^/]+([^\"]+)\"", "href=\"http://www.123.com/text$1\"");
      

  5.   

    正则
    (?<=(?:src|href|url|background)\s*=\s*["'])\.\.(.*?)(?=["']>)
    替换
    http://www.123.com/text$1
    测试成功
      

  6.   

    谢谢楼上的 你这样做并不能满足我的需求行了我自己已经解决了表达式
    ((?:src|href|url|background)(?:\s*=\s*(["']?)))(?!(?:http://|/|#|javascript))([^\s\2>]*)
    替换
    $1http://www.123.com/test/$3获取:以 src 或 href 或 url 或 background 为开头的属性
    判断 "="  忽略"="前后的空格
    判断是否有 双引号 或 单引号 并在 连接字符串关闭时 相对应
    并返回连接
    判断 连接 不是以 "http://" 或 "/" 或 # 或 javascript 开头 
    即 连接是相对连接替换
    将连接替换为绝对连接需要 IgnoreCase基本上大部分情况都已经解决了,谢谢大家参与.
      

  7.   

    不好意思测试过之后发现点问题
    ((?:src|href|url|background)(?:\s*=\s*(["']?)))(?!(?:http://|/|#|javascript))([^\s\2>]*)
    在遇到连接 是http:// 或 / 或 # 或 javascript开头的时候 
    仍然能匹配 ((?:src|href|url|background)(?:\s*=\s*(["']?)))
    所以使用System.Text.RegularExpressions.Regex.Replace()方法替换时仍然会插入http://www.123.com/test/ 导致出错
    后来我改了下去处了这个错误:
    (?<=((?:src|href|url|background)(?:\s*=\s*(["']?)))(?!http://|/|#|javascript))([^\s"'>]*)
    用上面这个就能正确剔除了