Javascript: regexp to remove .html
Javascript: regexp to remove .html
我正在寻找一些删除字符串.html扩展名的正则表达式。
由于某种原因,我已经找到了这两个它们不起作用:
var path = './views/contacts/node/item.html';
var otherPath = path;
path.replace(/'.[^/.]+$/, '');
console.log(path);
// returns ./views/contacts/node/item.html
otherPath.replace(/(.*)'.[^.]+$/, '');
console.log(otherPath);
// also returns ./views/contacts/node/item.html
知道出了什么问题吗?
您的原始regex
有效,您只是没有捕获返回结果。以下两种方法都将起作用:
path = path.replace(/'.[^/.]+$/, '');
path = path.replace(/'.html$/, '');
http://jsfiddle.net/AE2BY/
path = path.replace(/'.html$/, '');
假设.html扩展名位于字符串的末尾,则始终如此。
至于你提到的正则表达式有什么问题——它们是不正确的,永远不会匹配你给出的示例字符串。
var path = './views/contacts/node/item.html';
var otherPath = path.replace(/('.html).*?$/, '');
console.log(otherPath) // './views/contacts/node/item'
这应该删除之后的所有内容,包括" .html
"。
没有从上面列出的函数中获得所需内容的原因是因为您console.log
错误的变量。改变:
var path = './views/contacts/node/item.html';
var otherPath = path;
path.replace(/'.[^/.]+$/, '');
console.log(path);
自:
var path = './views/contacts/node/item.html';
var otherPath = path.replace(/'.[^/.]+$/, '');
console.log(otherPath);
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何设置html元素填充的动画
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何编写HTML输入的JS内联
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 使用javascript将动态表从一个html页面打印到另一个html页
- Javascript: regexp to remove .html
- AngularJS: ng-bind-html remove styling
- .remove() 只删除内部 HTML,而不是像我想要的那样删除整个标签
- html()、append()对通过ajax调用来的元素不起作用,remove起作用
- Remove index.html GWT
- JQuery .empty(), .remove() or .html('')
- Regexp to remove all html tags except <br>
- 按钮在执行名为remove()的函数后从html页面中删除
- Remove "|" from html
- Javascript remove HTML
- Remove from html
- 使用对html元素的引用,使用Jquery添加Remove元素
- angularjs remove index.html