Javascript 将匹配字符串中最后一次出现的点的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RegEx that will match the last occurrence of dot in a string
提问by alt
I have a filename that can have multiple dots in it and could end with any extension:
我有一个文件名,其中可以有多个点,并且可以以任何扩展名结尾:
tro.lo.lo.lo.lo.lo.png
I need to use a regex to replace the last occurrence of the dot with another string like @2x
and then the dot again (very much like a retina image filename) i.e.:
我需要使用正则表达式将最后一次出现的点替换为另一个字符串@2x
,然后再次替换点(非常像视网膜图像文件名),即:
tro.lo.png -> [email protected]
Here's what I have so far but it won't match anything...
这是我到目前为止所拥有的,但它不会匹配任何东西......
str = "http://example.com/image.png";
str.replace(/.([^.]*)$/, " @2x.");
any suggestions?
有什么建议?
回答by Jon
You do not need a regex for this. String.lastIndexOf
will do.
您不需要为此使用正则表达式。String.lastIndexOf
会做。
var str = 'tro.lo.lo.lo.lo.lo.zip';
var i = str.lastIndexOf('.');
if (i != -1) {
str = str.substr(0, i) + "@2x" + str.substr(i);
}
Update:A regex solution, just for the fun of it:
更新:一个正则表达式解决方案,只是为了好玩:
str = str.replace(/\.(?=[^.]*$)/, "@2x.");
Matches a literal dot and then asserts ((?=)
is positive lookahead) that no other character up to the end of the string is a dot. The replacement should include the one dot that was matched, unless you want to remove it.
匹配一个文字点,然后断言((?=)
是正向前瞻)直到字符串末尾没有其他字符是点。替换应包括匹配的一个点,除非您想删除它。
回答by Salman A
Just use special replacement pattern$1
in the replacement string:
只需在替换字符串中使用特殊的替换模式$1
:
console.log("tro.lo.lo.lo.lo.lo.png".replace(/\.([^.]+)$/, "@2x."));
// "[email protected]"
回答by Salman A
You can use the expression \.([^.]*?)
:
您可以使用表达式\.([^.]*?)
:
str.replace(/\.([^.]*?)$/, "@2x.");
You need to reference the $1
subgroup to copy the portion back into the resulting string.
您需要引用$1
子组以将该部分复制回结果字符串。
回答by Tats_innit
working demohttp://jsfiddle.net/AbDyh/1/
工作演示http://jsfiddle.net/AbDyh/1/
code
代码
var str = 'tro.lo.lo.lo.lo.lo.zip',
replacement = '@2x.';
str = str.replace(/.([^.]*)$/, replacement + '');
$('.test').html(str);
alert(str);
?
回答by JRS
To match all characters from the beginning of the string until (and including) the last occurence of a character use:
要匹配从字符串开头到(并包括)最后一次出现的所有字符,请使用:
^.*\.(?=[^.]*$) To match the last occurrence of the "." character
^.*_(?=[^.]*$) To match the last occurrence of the "_" character
回答by linepogl
Use \.
to match a dot. The character .
matches anycharacter.
使用\.
匹配点。该字符.
匹配任何字符。
Therefore str.replace(/\.([^\.]*)$/, ' @2x.')
.
因此str.replace(/\.([^\.]*)$/, ' @2x.')
。
回答by Avinash Raj
You could simply do like this,
你可以简单地这样做,
> "tro.lo.lo.lo.lo.lo.zip".replace(/^(.*)\./, "@2x");
'tro.lo.lo.lo.lo.lo@2xzip'
回答by J_A_X
Why not simply split the string and add said suffix to the second to last entry:
为什么不简单地拆分字符串并将所述后缀添加到倒数第二个条目:
var arr = 'tro.lo.lo.lo.lo.lo.zip'.split('.');
arr[arr.length-2] += '@2x';
var newString = arr.join('.');
回答by ???
'tro.lo.lo.lo.lo.lo.png'.replace(/([^\.]+).+(\.[^.]+)/, ".@x2")