如何在Javascript中使用正则表达式匹配定义的模式(How to match defined pattern using regular expression in Javascript)
IT问题网 2021-02-22 00:00:00
问 题
嗨,
首先,有一个字符串叫做:
var bookpath = ' ../ mybook / computer / sample_1 / sample_1_p16.pdf';
这是我的正则表达式:
bookpath = bookpath.replace(/sample.+pdf/gi, " ");
最后,结果bookpath的内容是"../mybook/computer/"。
如何获得"../mybook/computer/sample_1/"?
非常感谢
解决方案
嗨,
如果有帮助的话,请看这个链接。
使用javascript获取url参数
解析查询字符串的简便方法您获取某些值的网址。
问候,
algem
以此为例:
lt; html gt;
lt; head gt; lt; title gt; 正则表达式测试lt; / title gt;
lt; script 类型 =" text / javascript" gt;
函数测试(值){
alert(value.replace(/ \w {2} \d {1,2} \。 pdf / i,""));
}
lt; / script gt;
lt; / head gt; lt; body gt;
lt; 表格 action =" " gt;
lt; input type =" 按钮" value =" 测试1" onclick =" 测试('../ mybook / computer / sample_1 / sample_1_p16.pdf')" gt;
lt; /表格 gt;
lt; / body gt; lt; / html gt;
最好的问候,>
-mrb
分享: