所以我有一些名字,例如:page_1.something.signature
我想将.signature
替换为.date
,因此我使用类似的内容:
var string = "page_1.something.signature"
string = string.replace('.signature', '.date');
但是如果我的字符串是page_1.signature.signature
怎么办?然后我的字符串最终成为page_1.date.signature
。
所以我的问题是,我如何仅将替换函数应用于字符串的末尾并忽略 .something ?
答案 0 :(得分:2)
var string = "page_1.something.signature"
string = string.replace(/\.signature$/, '.date');
此方法使用正则表达式。最后的$
表示字符串必须以之前的模式结束。如果你之前没有使用正则表达式,那么就值得阅读一下,并且这样做很简单。
答案 1 :(得分:0)
虽然你已经接受了答案,但我认为我会提供一种更实用的方法来实现类似的结果,这种方法取代最后一次出现的传递给函数的字符串(无论是它在字符串的末尾,或者其他)。也就是说,这是函数:
function replaceLastOccurrence(haystack, needle, replacement) {
// if you don't supply the required arguments, or the string to
// replace is not in the string we pass to the function) we do nothing
// I chose to return haystack (if it exists) or false (if it does not):
if (!haystack || !needle || !replacement || haystack.indexOf(needle) === -1) {
return haystack || false;
}
// breaking original string into an array of single-characters:
var newString = haystack.split('')
// using Array.prototype.splice() to manipulate the array,
// finding the sequence of characters beginning at the lastIndexOf()
// the string you're looking to replace, removing the array-elements
// equal to the length of the string you want to replace (needle.length)
// and replacing those array elements with the replacement-string:
newString.splice(haystack.lastIndexOf(needle), needle.length, replacement)
// because splice manipulates the original array, but returns
// the array-elements it removes, we here join the array back
// together to form a String using Array.prototype.join(''):
return newString.join('');
}
var string = 'page_1.something.signature.more.signature.keys',
newString = replaceLastOccurrence(string, 'signature', 'date');
function replaceLastOccurrence(haystack, needle, replacement) {
if (!haystack || !needle || !replacement || haystack.indexOf(needle) === -1) {
return haystack || false;
}
var newString = haystack.split('')
newString.splice(haystack.lastIndexOf(needle), needle.length, replacement)
return newString.join('');
}
var string = 'page_1.something.signature.more.signature.keys',
newString = replaceLastOccurrence(string, 'signature', 'date');
console.log(newString); // 'page_1.something.signature.more.date.keys'
演示几个字符串的输出:
function replaceLastOccurrence(haystack, needle, replacement) {
if (!haystack || !needle || !replacement || haystack.indexOf(needle) === -1) {
return haystack || false;
}
var newString = haystack.split('')
newString.splice(haystack.lastIndexOf(needle), needle.length, replacement);
return newString.join('');
}
var originals = document.querySelectorAll('.original');
Array.prototype.forEach.call(originals, function(original) {
original.nextElementSibling.textContent = replaceLastOccurrence(original.textContent, 'signature', 'date');
});
ul,
li {
list-style-type: none;
}
ul {
margin-top: 4em;
}
li {
border-bottom: 1px solid #ccc;
padding: 0.2em 0.5em;
margin: 0 0 0.5em 0;
}
.original::after {
content: ': ';
}
.output {
float: right;
}
<ul>
<li><span class="original">something</span><span class="output"></span>
</li>
<li><span class="original">page_1.something.signature</span><span class="output"></span>
</li>
<li><span class="original">page_1.something.signature.another</span><span class="output"></span>
</li>
<li><span class="original">page_1.something.signature.signature</span><span class="output"></span>
</li>
</ul>
参考文献: