如何在Javascript中使用正则表达式匹配定义的模式 [英] How to match defined pattern using regular expression in Javascript

查看:79
本文介绍了如何在Javascript中使用正则表达式匹配定义的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,有一个字符串叫做:

Hi,

First, there is a string called:

var bookPath = '../MyBook/Computer/Sample_1/Sample_1_p16.pdf';


这是我的正则表达式:


And here is my regular expression:

bookPath = bookPath.replace( /Sample.+pdf/gi, "" );


最后,bookPath的结果是"../MyBook/Computer/".
如何获取"../MyBook/Computer/Sample_1/"呢?

非常感谢〜


Finally, the result of bookPath is "../MyBook/Computer/".
How do I get "../MyBook/Computer/Sample_1/" instead?

Many thanks~

推荐答案



如果有帮助,请参见此链接.
使用Javascript获取URL参数
一种解析URL中的查询字符串以获取某些值的简单方法.


问候,

代数
Hi,

See this link if it could help.
Get URL Parameters Using Javascript
An easy way to parse the query string in your URL to grab certain values.


Regards,

Algem


以这个为例:

Use this for an example:

<html>
<head><title>Regular Expression Test</title>
<script type="text/javascript">
function Test (value) {
    alert(value.replace(/\w{2}\d{1,2}\.pdf/i, ""));
}
</script>
</head><body>
<form action="">
<input type="button" value="Test 1" onclick="Test('../MyBook/Computer/Sample_1/Sample_1_p16.pdf')">
</form>
</body></html>



最好的问候,

—MRB



Best Regards,

—MRB


这篇关于如何在Javascript中使用正则表达式匹配定义的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆