简体   繁体   English

如何使用 JavaScript 动态方法从右侧删除/替换第一次出现的字符?

[英]How to remove/replace first occurrence of character from the right using JavaScript dynamic approach?

example例子

var string = "ACABBCAA";
var a = "A";
var b = "B";
var c= "C";

output:输出:

removeLastChar(string,a);  //output ACABBCA
removeLastChar(string,b);  //output ACABCAA
removeLastChar(string,c);  //output ACABBAA

What I have tried so far?到目前为止我尝试过什么?

Solution 1解决方案1

 function removeLastChar(string,char){
        result = string.replace(new RegExp('/'+char+'$/'), "");
        return result;
    }

Solution 2解决方案2

function removeLastChar(string,char){
    result = string.replace('/'+char+'$/', "");
    return result;
}

I already asked in the comments here but not working out.我已经在这里的评论中过,但没有解决。

This is my way to do it这是我的方法

var text = "ACABBCAA";
var a = "A";
var b = "B";
var c= "C";
function removeLastChar(string,char){
  let charLastPosition = string.lastIndexOf(char);
  let newString = string.substring(0, charLastPosition) + string.substring(charLastPosition + 1);
  return newString;
}

document.write(removeLastChar(text, a));

if you wanna replace you can do it un this way.如果你想更换,你可以这样做。

var text = "Notion,Data,Identity,";
var a = "A";
var b = "B";
var c= "C";
function replaceLastChar(string,char, charToReplace){
    let charLastPosition = string.lastIndexOf(char);
    let newString = string.substring(0, charLastPosition) + charToReplace + string.substring(charLastPosition + 1);
  return newString;
}

document.write(replaceLastChar(text, ',', '.'));

Use lastIndexOf and splice with spread notation ... .使用lastIndexOfsplice with spread notation ...

function removeLastChar(string, char) {
  let strArr = [...string];
  strArr.splice(string.lastIndexOf(char), 1);
  return strArr.join("");
}

You could use a dynamic regex like this: ${char}(?=[^${char}]*$) (For B it would be: B(?=[^B]+$) ).您可以使用这样的动态正则表达式: ${char}(?=[^${char}]*$) (对于B将是: B(?=[^B]+$) )。 This will match the last instace of a character and replace it with empty string这将匹配字符的最后一个实例并将其replace为空字符串

 function removeLastChar(str, char) { const regex = new RegExp(`${char}(?=[^${char}]*$)`) return str.replace(regex, '') } console.log(removeLastChar("ACABBCAA", "A")); console.log(removeLastChar("ACABBCAA", "B")); console.log(removeLastChar("ACABBCAA", "C"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用JavaScript中的字符串替换第一次出现的字符? - How to replace the first occurrence of a character with a string in JavaScript? Javascript:如何从值中删除字符的出现? - Javascript:How to remove occurrence of a character from a value? JavaScript RegEx-替换字符的第一个和最后一个出现 - JavaScript RegEx - replace first and last occurrence of a character 如何使用 javascript/typescript 从字符串中第一次出现“_”之前删除文本? - How do i remove a text before the first occurrence of “_” from a string using javascript/typescript? 在字符串 javascript 的左右找到第一次出现的字符 - find first occurrence of character in left and right of string javascript 如何删除Javascript字符串中所有出现的字符'? - How to remove all occurrence of character ' in a string in Javascript? C# - 如何使用 C# 或正则表达式查找和替换每第 n 个字符出现? 从 Javascript 到 C# 的转换 - C# - How to find and replace every nth character occurrence using C# or regex? Conversion from Javascript to C# 我如何使用javascript替换字符串右端的第x个字符? - how do i replace the x'th character from the right end of a string using javascript? 仅将首次出现的内容替换为JavaScript - Replace only first occurrence with javascript 如何使用JavaScript(html)删除某个字符出现之前/之后的所有内容 - How to remove everything before/after the occurrence of a certain character using JavaScript (html)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM