问题描述
我有一个网址:
http://test.com/backgrounds/testimage.jpg
,我需要在文件名的开头插入一个"t_",结果是:
and I need to insert a 't_' at the beginning of the filename resulting in:
http://test.com/backgrounds/t_testimage.jpg
在jQuery中最简单的方法是什么?感谢您的帮助:)
What would be the simplest way to do this in jQuery? Thanks for any help :)
推荐答案
使用正则表达式替换查找最后的"/",并将其替换为"/t_".难以阅读的转义符,但:
Use a regex replacement to look for the last "/" and replace it with "/t_". Difficult to read with the escapes but:
"http://test.com/backgrounds/testimage.jpg".replace(/\/([^\/]*)$/, "/t_$1");
将字符串与要操作的任何其他字符串或变量交换出去.如果您想获得更多控制权或只是希望提高可读性,则可以使用其他方法来解析URL.
Swap out the string with whatever other string or variable you want to operate on.Alternately use something to parse the URL if you want more control or just prefer more readability.
或者,您也可以从模式和替换中删除"/".我希望JS具有非正则表达式replaceLast,但事实并非如此.
Or alternately you could also remove the "/" from the pattern and the substitution. I was hoping JS had a non-regex replaceLast but that doesn't seem to be the case.
这篇关于jQuery在最后一个/后面的字符串中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!