Replace last occurrence of character in string
You don't need jQuery, just a regular expression.
This will remove the last underscore:
var str = 'a_b_c';console.log( str.replace(/_([^_]*)$/, '$1') ) //a_bc
How to replace the last instance of a character in a string?
You have a _
instead of a ,
and you've wrapped your regex in quotes. I think you'll also need to add a space before the and
:
myString = myString.replace(/,([^,]*)$/,'\ and$1');
Edit:
You could also do this without regex, if you're so inclined:
str = "Maria, David, Charles, Natalie";
lastComma = str.lastIndexOf(',');
newStr = str.substring(0, lastComma) + ' and' + str.substring(lastComma + 1);
//=> "Maria, David, Charles and Natalie"
Replace last character of string using JavaScript
You can do it with regex easily,
var str1 = "Notion,Data,Identity,".replace(/.$/,".")
.$
will match any character at the end of a string. JavaScript: replace last occurrence of text in a string
Well, if the string really ends with the pattern, you could do this:
str = str.replace(new RegExp(list[i] + '$'), 'finish');
Replace last occurrence word in javascript
Here is an idea ....
This is a case-sensitive string search version
var str = 'abc def abc xyz';
var word = 'abc';
var newWord = 'test';
// find the index of last time word was used
// please note lastIndexOf() is case sensitive
var n = str.lastIndexOf(word);
// slice the string in 2, one from the start to the lastIndexOf
// and then replace the word in the rest
str = str.slice(0, n) + str.slice(n).replace(word, newWord);
// result abc def test xyz
If you want a case-insensitive version, then the code has to be altered. Let me know and I can alter it for you. (PS. I am doing it so I will post it shortly)Update: Here is a case-insensitive string search version
var str = 'abc def AbC xyz';
var word = 'abc';
var newWord = 'test';
// find the index of last time word was used
var n = str.toLowerCase().lastIndexOf(word.toLowerCase());
// slice the string in 2, one from the start to the lastIndexOf
// and then replace the word in the rest
var pat = new RegExp(word, 'i')
str = str.slice(0, n) + str.slice(n).replace(pat, newWord);
// result abc def test xyz
N.B. Above codes looks for a string. not whole word (ie with word boundaries in RegEx). If the string has to be a whole word, then it has to be reworked.Update 2: Here is a case-insensitive whole word match version with RegEx
var str = 'abc def AbC abcde xyz';
var word = 'abc';
var newWord = 'test';
var pat = new RegExp('(\\b' + word + '\\b)(?!.*\\b\\1\\b)', 'i');
str = str.replace(pat, newWord);
// result abc def test abcde xyz
Good luck:)
javascript replace last occurrence of string
newString = oldString.substring(0,oldString.lastIndexOf("_")) + 'aa';
Create a regex to replace the last occurrence of a character in a string
Instead of RegEx, use String#substring
and String.lastIndexOf
as below
const str = 'Field Name* * ';const replaceStr = 'mandatory';const lastIndex = str.lastIndexOf('*');const result = str.substring(0, lastIndex) + replaceStr + str.substring(lastIndex + 1);
console.log('Substitution result: ', result);
How to replace last occurrence of characters in a string using javascript
foo.replace(/,([^,]*)$/, ' and $1')
use the $
(end of line) anchor to give you your position, and look for a pattern to the right of the comma index which does not include any further commas.Edit:
The above works exactly for the requirements defined (though the replacement string is arbitrarily loose) but based on criticism from comments the below better reflects the spirit of the original requirement.
console.log( 'test1, test2, test3'.replace(/,\s([^,]+)$/, ' and $1') )
JavaScript RegEx - replace first and last occurrence of a character
You can use the following to replace the first and last {
and }
var s='/claims/{abc{de}}f}';
document.write(s.replace(/{(.*)}/,':$1'));
Related Topics
Render Object Properties in React
Jquery Document.Ready VS Phonegap Deviceready
JavaScript Replace() Method Dollar Signs
How to Chain These Functions Together with Promises
JavaScript "Variable Variables": How to Assign Variable Based on Another Variable
How to Take Advantage of Callback Functions for Asynchronous Xmlhttprequest
Cross Domain Localstorage with JavaScript
Do Browsers Parse JavaScript on Every Page Load
Subject VS Behaviorsubject VS Replaysubject in Angular
Pass Post Data with Window.Location.Href
How to Use Local Storage in Angular
Handling Key-Press Events (F1-F12) Using JavaScript and Jquery, Cross-Browser
How to Send the "&" (Ampersand) Character via Ajax
JavaScript Timestamp to Relative Time
Javascript: Dynamically Creating Variables for Loops