简体   繁体   English

需要一些正则表达式帮助JavaScript

[英]need some regex help javascript

I am hoping that someone can help me, I am using Javascript and asp 我希望有人可以帮助我,我正在使用Javascript和ASP

I have this string /folder1/folder2/pagename.asp 我有这个字符串/folder1/folder2/pagename.asp

I need a regex to strip the /pagename.asp from the string 我需要一个正则表达式从字符串中删除/pagename.asp

pagetype.replace(/\/.*?\.asp/ig, '');

The regex above is halfway there but the problem is that it strippes from the beginning / everything between the / and the .asp 上面的正则表达式位于中间,但问题是它从开头剥夺了/和.asp之间的所有内容

how do I make the regex lazy so that it will only strip between the last / and the .asp? 我如何使正则表达式变得懒惰,使其仅在last /和.asp之间剥离?

Any help would be appreciated! 任何帮助,将不胜感激!

/(.*)\/.*?\.asp/ig

The problem is that the regex is matching as early as possible - one solution is to put a greedy quantifier at the start so that it gobbles up all the possible early matches and you get the last one. 问题是正则表达式要尽早匹配-一种解决方案是在开头放置一个贪婪的量词,以便吞噬所有可能的早期匹配,然后得到最后一个。

And we use a capturing group to grab that bit so we can keep it around. 然后,我们使用一个捕获组来抓取该位,以便将其保留。

Try this: 尝试这个:

pagetype.replace(/\/[^\/]*\.asp/ig, '');

I've used [^\\/] group to represent any symbol not equal to / . 我用[^\\/]组表示任何不等于/符号。

确定使用此方法,将删除页面名称和扩展名:


(\w+)(\.\w+)(:[0-9]+)?(/.+)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM