替换字符串中最后出现的字符

时间:2021-09-05 07:47:41

Is there an easy way in jQuery to replace the last occurrence of an '_' (underscore) in a given string?

在jQuery中是否有一个简单的方法来替换给定字符串中最后一个“_”(下划线)的出现?

8 个解决方案

#1


94  

You don't need jQuery, just a regular expression.

您不需要jQuery,只需要一个正则表达式。

This will remove the last underscore:

这将删除最后一个下划线:

var str = 'a_b_c';
str = str.replace(/_([^_]*)$/,'$1'); //a_bc

This will replace it with the contents of the variable replacement:

这将替换为变量替换的内容:

var str = 'a_b_c', replacement = '!';
str = str.replace(/_([^_]*)$/,replacement+'$1'); //a_b!c

#2


52  

No need for jQuery nor regex

不需要jQuery或regex

str = str.substring(0,str.length-2)+otherchar

str = str.substring(0,str.length-2)+ otherchar

to replace last char in a string

替换字符串中的最后一个字符

var pos = str.lastIndexOf('_');
str = str.substring(0,pos) + otherchar + str.substring(pos+1)

to replace last underscore

取代最后强调

or use one of the regular expressions you will get in a minute from someone :)

或者使用一个你马上就会从某人那里得到的正则表达式:

#3


4  

What about this?

这是什么?

function replaceLast(x, y, z){
  var a = x.split("");
  a[x.lastIndexOf(y)] = z;
  return a.join("");
}

replaceLast("Hello world!", "l", "x"); // Hello worxd!

#4


2  

Reverse the string, replace the char, reverse the string.

反转字符串,替换字符,反转字符串。

Here is a post for reversing a string in javascript: How do you reverse a string in place in JavaScript?

这里有一个在javascript中反转字符串的帖子:如何在javascript中反转字符串?

#5


2  

Keep it simple

保持简单

var someString = "a_b_c";
var newCharacter = "+";

var newString = someString.substring(0, someString.lastIndexOf('_')) + newCharacter + someString.substring(someString.lastIndexOf('_')+1);

#6


1  

This is very similar to mplungjan's answer, but can be a bit easier (especially if you need to do other string manipulation right after and want to keep it as an array) Anyway, I just thought I'd put it out there in case someone prefers it.

这与mplungjan的答案非常相似,但是可以更容易一些(特别是如果您需要在以后执行其他字符串操作,并希望将其作为数组),我只是想把它放在那里,以防有人喜欢它。

var str = 'a_b_c';
str = str.split(''); //['a','_','b','_','c']
str.splice(str.lastIndexOf('_'),1,'-'); //['a','_','b','-','c']
str = str.join(''); //'a_b-c'

The '_' can be swapped out with the char you want to replace

可以用要替换的字符替换“_”

And the '-' can be replaced with the char or string you want to replace it with

“-”可以替换为要替换的字符或字符串

#7


0  

You can use this code

您可以使用此代码。

var str="test_String_ABC";
var strReplacedWith=" and ";
var currentIndex = str.lastIndexOf("_");
str = str.substring(0, currentIndex) + strReplacedWith + str.substring(currentIndex + 1, str.length);

alert(str);

#8


0  

    // Define variables
    let haystack = 'I do not want to replace this, but this'
    let needle = 'this'
    let replacement = 'hey it works :)'
    
    // Reverse it
    haystack = Array.from(haystack).reverse().join('')
    needle = Array.from(needle).reverse().join('')
    replacement = Array.from(replacement).reverse().join('')
    
    // Make the replacement
    haystack = haystack.replace(needle, replacement)
    
    // Reverse it back
    let results = Array.from(haystack).reverse().join('')
    console.log(results)
    // 'I do not want to replace this, but hey it works :)'

#1


94  

You don't need jQuery, just a regular expression.

您不需要jQuery,只需要一个正则表达式。

This will remove the last underscore:

这将删除最后一个下划线:

var str = 'a_b_c';
str = str.replace(/_([^_]*)$/,'$1'); //a_bc

This will replace it with the contents of the variable replacement:

这将替换为变量替换的内容:

var str = 'a_b_c', replacement = '!';
str = str.replace(/_([^_]*)$/,replacement+'$1'); //a_b!c

#2


52  

No need for jQuery nor regex

不需要jQuery或regex

str = str.substring(0,str.length-2)+otherchar

str = str.substring(0,str.length-2)+ otherchar

to replace last char in a string

替换字符串中的最后一个字符

var pos = str.lastIndexOf('_');
str = str.substring(0,pos) + otherchar + str.substring(pos+1)

to replace last underscore

取代最后强调

or use one of the regular expressions you will get in a minute from someone :)

或者使用一个你马上就会从某人那里得到的正则表达式:

#3


4  

What about this?

这是什么?

function replaceLast(x, y, z){
  var a = x.split("");
  a[x.lastIndexOf(y)] = z;
  return a.join("");
}

replaceLast("Hello world!", "l", "x"); // Hello worxd!

#4


2  

Reverse the string, replace the char, reverse the string.

反转字符串,替换字符,反转字符串。

Here is a post for reversing a string in javascript: How do you reverse a string in place in JavaScript?

这里有一个在javascript中反转字符串的帖子:如何在javascript中反转字符串?

#5


2  

Keep it simple

保持简单

var someString = "a_b_c";
var newCharacter = "+";

var newString = someString.substring(0, someString.lastIndexOf('_')) + newCharacter + someString.substring(someString.lastIndexOf('_')+1);

#6


1  

This is very similar to mplungjan's answer, but can be a bit easier (especially if you need to do other string manipulation right after and want to keep it as an array) Anyway, I just thought I'd put it out there in case someone prefers it.

这与mplungjan的答案非常相似,但是可以更容易一些(特别是如果您需要在以后执行其他字符串操作,并希望将其作为数组),我只是想把它放在那里,以防有人喜欢它。

var str = 'a_b_c';
str = str.split(''); //['a','_','b','_','c']
str.splice(str.lastIndexOf('_'),1,'-'); //['a','_','b','-','c']
str = str.join(''); //'a_b-c'

The '_' can be swapped out with the char you want to replace

可以用要替换的字符替换“_”

And the '-' can be replaced with the char or string you want to replace it with

“-”可以替换为要替换的字符或字符串

#7


0  

You can use this code

您可以使用此代码。

var str="test_String_ABC";
var strReplacedWith=" and ";
var currentIndex = str.lastIndexOf("_");
str = str.substring(0, currentIndex) + strReplacedWith + str.substring(currentIndex + 1, str.length);

alert(str);

#8


0  

    // Define variables
    let haystack = 'I do not want to replace this, but this'
    let needle = 'this'
    let replacement = 'hey it works :)'
    
    // Reverse it
    haystack = Array.from(haystack).reverse().join('')
    needle = Array.from(needle).reverse().join('')
    replacement = Array.from(replacement).reverse().join('')
    
    // Make the replacement
    haystack = haystack.replace(needle, replacement)
    
    // Reverse it back
    let results = Array.from(haystack).reverse().join('')
    console.log(results)
    // 'I do not want to replace this, but hey it works :)'