Reputation: 9399
So I have a few names such as: page_1.something.signature
I want to replace .signature
with .date
so I use something like:
var string = "page_1.something.signature"
string = string.replace('.signature', '.date');
But what if my string is page_1.signature.signature
? Then my string ends up being page_1.date.signature
.
So my question is, how do I only apply the replace function to the end of the string and ignore the .something?
Upvotes: 0
Views: 153
Reputation: 253416
While you've already accepted an answer, I thought I'd offer a more functional approach to achieve a similar result, this approach replaces the last occurrence of the string passed to the function (whether it's at the end of the string, or otherwise). That said, here's the function:
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'
To demonstrate the output of several strings:
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>
References:
Array.prototype.forEach()
.Array.prototype.join()
.Array.prototype.splice()
.Function.prototype.call()
.NonDocumentTypeChildNode.nextElementSibling
.String.prototype.lastIndexOf()
.String.prototype.split()
.Upvotes: 0
Reputation: 5776
var string = "page_1.something.signature"
string = string.replace(/\.signature$/, '.date');
This approach uses a regular expression. The $
at the end means the string must end with the pattern before it. If you've not used regular expressions before , they're worth reading a about and make things like this easy.
Upvotes: 3