我有一个字符串' http://this.is.my.url:007/directory1/directory2/index.html ',我需要提取字符串,如下所示.请建议最好的方法
var one = http://this.is.my.url:007/directory1/directory2/index
试试这个:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.[^.]*$/g, ''); // would replace all file extensions at the end.
// or in case you only want to remove .html, do this:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.html$/g, '');
Run Code Online (Sandbox Code Playgroud)
包含在正则表达式中的$字符与文本字符串的末尾匹配.在变体a中,你看起来是"." 并删除此字符中的所有内容,直到字符串结束.在变体2中,您将其减少为精确的字符串".html".这更多是关于正则表达式而不是关于javascript.要了解有关它的更多信息,这里有许多很好的教程.