angular - 删除最后一次出现的字符`"`的字符串操作
问题描述
我有这样的情况,如果给定的字符串有奇数个引号,"
那么最后一个引号必须用空字符串替换。这是我的代码,我遵循了一种方法来实现这一目标,但它没有替换字符串,任何人都可以帮我吗?
const input = `"hello,"sai,sur",ya,teja`;
let output = "";
if(evenOrOdd(input.split(`"`) == "even")){
//Here the last occurrence which needed to be replaced with empty string
input[input.split(`"`).lastIndexOf(`"`)] = "";
console.log(input);
output = input.replace(/"([^"]+)"/g, (_, g) => g.replace(',', '-'))
}else{
output = input.replace(/"([^"]+)"/g, (_, g) => g.replace(',', '-'))
}
console.log(output);
function evenOrOdd(number){
//check if the number is even
if(number % 2 == 0) {
console.log("The number is even.");
return "even";
}
// if the number is odd
else {
console.log("The number is odd.");
return "odd";
}
}
Thanks in advance :)
解决方案
您可以像下面这样替换:
const input = `"hello,"sai,sur",ya,teja`;
const idx = input.lastIndexOf('"');
const result = input.substr(0, idx) + input.substr(idx+1)
console.log(result);
推荐阅读
- html - 为什么我们不能使用 CSS 自定义 HTML 中选择选项的突出显示颜色?
- c# - 使用自定义过滤器在表单中输入 HTML 时 ASP.NET MVC 处理错误
- perl - 将导入的函数传递给 Perl 中的另一个包
- python - 数据框:如何为每一行选择不同的列
- r - 如何解决 RSelenium 错误消息“无法连接到 localhost 端口 4444:连接被拒绝”?
- url - 健康检查是否应该调用其他应用健康检查
- apache-httpcomponents - 监控 PoolingHttpClientConnectionManager 的连接池
- jq - 使用jq根据json数组中的变量值选择对象
- haskell - 管道两个子流程时的异常行为
- java - 是否可以让 DriverManager.setLoginTimeout(int value) 以毫秒为单位接受超时时间?