查看“︁Regular/替换域名”︁的源代码
来自康健生活
←
Regular/替换域名
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 取网址 == 方法一: <syntaxhighlight lang="javascript"> /(http|https):\/\/([\w.]+\/?)\S*/</syntaxhighlight> 方法二: <syntaxhighlight lang="javascript"> /http[s]{0,1}:\/\/([\w.]+\/?)\S*/</syntaxhighlight> 使用 js 提取域名(带协议) <syntaxhighlight lang="javascript"> url.replace(/(http[s]{0,1}:\/\/[a-zA-Z0-9.-]+)?.*/i,'$1')</syntaxhighlight> 方法三: {{Double-columns|<syntaxhighlight lang="javascript"> const str = 'http://s3.qhmsg.com/qs/;mapsapi;m/1001.js'; const reg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/; const result= str.replace(reg,"kangkk.com"); console.log(result); </syntaxhighlight>|{{consoleout|<pre>'http://kangkk.com/qs/;mapsapi;m/1001.js' </pre>}}}} == PHP == 正则提取的url中的域名以及替换域名的方法 preg_match()和preg_replace() <syntaxhighlight lang="php"> <?php $url = 'http://www.baidu.com/index.php'; //网站的url $reg = '/(http):\/\/([^\/]+)/i'; //正则表达式 preg_match($reg, $url,$res); ?></syntaxhighlight> <blockquote><pre> $res的结果 array (size=3) => string 'http://www.baidu.com' (length=20) => string 'http' (length=4) => string 'www.baidu.com' (length=13) </pre></blockquote> <syntaxhighlight lang="php"> <?php $url1 = 'www.jingdong.com'; /** Example #1 使用后向引用紧跟数值原文 */ echo preg_replace($reg, 'http://'.$url1, $url); /** Example #2 preg_replace()中使用基于索引的数组 */ $patterns[0] = '/'.$res[2].'/'; $replacements[0] = $url1; echo preg_replace($patterns, $replacements, $url); ?></syntaxhighlight> <blockquote><pre> http://www.jingdong.com/index.php </pre></blockquote>
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
返回
Regular/替换域名
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。