Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:regexp以删除.html_Javascript_Regex_Replace - Fatal编程技术网

Javascript:regexp以删除.html

Javascript:regexp以删除.html,javascript,regex,replace,Javascript,Regex,Replace,我正在搜索一些删除字符串.html扩展名的regexp 我已经发现这两个因为某种原因它们不起作用: var path = './views/contacts/node/item.html'; var otherPath = path; path.replace(/\.[^/.]+$/, ''); console.log(path); // returns ./views/contacts/node/item.html otherPath.replace(/(.*)\.[^.]+$/, '');

我正在搜索一些删除字符串.html扩展名的regexp

我已经发现这两个因为某种原因它们不起作用:

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$/, '');

您原来的
regex
工作正常,只是没有捕获返回结果。以下两种方法都有效:

path = path.replace(/\.[^/.]+$/, '');

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扩展名位于字符串的末尾,则始终为

至于你提到的正则表达式有什么问题——它们是不正确的,永远不会与你给出的示例字符串匹配

var path = './views/contacts/node/item.html';
var otherPath = path.replace(/(\.html).*?$/, '');

console.log(otherPath) // './views/contacts/node/item'
这将删除“
.html
”之后的所有内容

您无法从上面列出的函数中获得所需的功能的原因是您使用了错误的变量。更改:

var path = './views/contacts/node/item.html';
var otherPath = path;
path.replace(/\.[^/.]+$/, '');
console.log(path);
致:

这将删除“
.html
”之后的所有内容

您无法从上面列出的函数中获得所需的功能的原因是您使用了错误的变量。更改:

var path = './views/contacts/node/item.html';
var otherPath = path;
path.replace(/\.[^/.]+$/, '');
console.log(path);
致:


也许您还可以详细说明原始正则表达式试图做什么以及为什么不起作用?我会将其更改为
/\.html$/
,以确保它只捕获扩展,但是这是正确的方法。他们应该删除文件扩展名也许你还可以详细说明原始正则表达式试图做什么以及为什么它们不工作?我会将其更改为
/\.html$/
,以确保它只捕获扩展名,但这是正确的方法。他们应该删除文件扩展名