javascript - 正则表达式匹配 / 和 .jsp 之间的字符串
问题描述
如果我做正则表达式匹配
str = "/mypage/account/info.jsp"
str.match('\/.*\.jsp')
我得到了整个字符串,但我只想获取“信息”
我怎样才能只使用正则表达式来完成?
解决方案
首先,你可以得到最后一个之后的文本/
/[^/]*$/
然后使用split得到想要的结果
const str = "/mypage/account/info.jsp";
const match = str.match(/[^/]*$/);
const result = match && match[0].split(".")[0];
console.log(result);
只有正则表达式
const str = "/mypage/account/info.jsp";
const match = str.match(/[^/]+(?=\.jsp$)/);
console.log(match[0]);
推荐阅读
- c# - 单击 html 元素时执行代码而不重定向或重新加载页面
- python - keras flow_from_directory 返回 0 张图片
- wordpress - 从特定页面中删除特定小部件
- python - 为什么 MailMerge 对象无法转换为 Unicode?是否有可靠的方法在 Python 中打印模板文档?
- javascript - 在 Dynamics 365 中使用 ADAL.js 获取没有弹出窗口的令牌
- pandas - 用 91 替换手机号码中的前导零
- javascript - 如何检查我的按钮是否在 JavaScript 中被点击?
- ember.js - 更新模型时如何将signed_id传递/隐藏到Rails API控制器
- docker - 如何使用 Dockerfile 在启动 docker 容器上运行 jboss-cli
- excel - 循环遍历 2 个数组 - 在表中查找 Header 然后将值粘贴到正确的位置