【问题标题】:Javascript replace substring doesn't work well if the string repeats如果字符串重复,Javascript 替换子字符串将无法正常工作
【发布时间】:2016-08-24 13:47:42
【问题描述】:

我对 substring/substr/slice 有一个非常简单的问题。我的代码很复杂所以这里就不贴了,这里是问题的简化版:

例如如何将 text.substring(5, 10) 更改为 "Something"?

text = "HelloHello";

如果我尝试

text = text.replace(text.substring(5,10), "Something");

结果将是“SomethingHello”,因为 text.substring(0, 5) 也是“Hello”,但我想得到“HelloSomething”

slice() 和 substr() 也不行,你能给我一个解决方案吗?

【问题讨论】:

  • text = text.substring(0,5) + "Something"; 有什么问题?
  • 好吧,我的代码很复杂,但真正的问题是:如果字符串之前已经出现在文本中,如何替换字符 text[a] 和 text[b] 之间的字符串?
  • 您的意思是要将两个索引值之间的所有文本替换为其他文本,还是要在两个索引值指定的子字符串中进行搜索并仅在该子字符串中进行替换?我可以想到两种方法来做到这一点,搜索字符串是否出现在指定范围之外并不重要。
  • 我想替换两个索引之间的所有文本。
  • 好的。好吧,使用 replace() 方法可以实现 ,但只执行 text = text.substring(0, start) + "Something" + text.substring(end); 会更简单。

标签: javascript replace substring slice substr


【解决方案1】:

基于下面的cmets,我写了函数replacePartOfText()

此函数将两个索引(开始,结束)之间的所有文本替换为单词

text = "HelloHello"

function replacePartOfText(text, start, end, word) {
  result = text.substring(0, start) + word + text.substring(end, text.length)

  return result
}

text = replacePartOfText(text, 5, 10, "Something")

console.log(text) // "HelloSomething"

【讨论】:

  • 这不会给HelloSomething。它将替换所有出现。
  • 事实是,我想根据数字 5 和 10 替换字符串。例如,您将如何替换此处的第 4 个 Hello:HelloHello Hello Hello Hello
  • @ArunGhosh - 它肯定不会取代所有的出现。如果您指定全局标志,正则表达式替换仅替换所有出现,但在任何情况下,此特定正则表达式仅匹配出现在字符串末尾的单词,所以...
  • @nnnnnn 我错过了$。但这仍然不是问题所要求的。它有来自基于位置的子字符串,这里没有。
  • @ArunGhosh 上面的检查功能。我改变了答案。
【解决方案2】:

为什么不使用splice 而不是所有这些复杂的解决方案?

var chars = "HelloHelloHello Hello HelloHello".split( "" );

chars.splice( 16, 5, "Something" );

alert( chars.join( "" ) );

清晰的功能、可读性和可维护性永远是赢家,这几乎是显而易见的。

【讨论】:

  • 这没关系,但如果我不仅需要为chars 重用此功能怎么办?我认为函数是更通用的解决方案。
  • 是的,这些函数是 JavaScript 的核心部分。它们被命名为 splitsplicejoin
【解决方案3】:
var text = "HelloHelloMahamadali.It is working now.";
var textbefore = text.substring(0,5);
var textafter = text.substring(5,text.length);
var texttoreplace = "Hello";
text = textbefore + textafter.replace(texttoreplace, "Something");
alert(text);

请使用以上一种逻辑,希望对您有所帮助。谢谢。欢迎发表评论。

【讨论】:

  • 如果这个答案对你有用,请你投票赞成吗?
  • 我没有足够的声誉,所以我的点赞不算数:/我是这里的新用户
【解决方案4】:

首先,让它工作:

为了满足这个需求:

"如何替换字符text[a]和text[b]之间的字符串"

function replaceStringPortion(text, start, end, replacement, trim) {
  text = !!trim ? text.trim() : text;
  var fragments = [
    text.substr(0, start),
    text.substr(start, end - start + 1),
    text.substr(end + 1, text.length)
  ];
  fragments[1] = replacement;
  return fragments.join('');
}

但是,为了满足您所描述的需求(替换 ab 之间的文本部分):

function replaceAfterStringPortion(text, start, end, replacement, trim) {
  text = !!trim ? text.trim() : text;
  var fragments = [
    text.substr(0, start),
    text.substr(start, end - start + 1),
    text.substr(end + 1, text.length)
  ];
  fragments[2] = replacement;
  return fragments.join('');
}

其次,.substr().replace() 工作得很好。原因如下:

如果你嵌套你的表达式命令可能会更清楚:

text = text.replace(
    text.substring(5,10), // Returns "Hello"
  "Something"
);

那么你在做什么,按照这个顺序:

  1. text.substring(5,10) 返回 "Hello"
  2. text.replace("Hello", "Something"); 正如预期的那样,返回 "SomethingHello"

这是预期的,因为 .replace() 替换了您传递的字符串或模式的第一次出现:

str.replace(regexp|substr, newSubStr|function)

...

substr(模式)

要被 newSubStr 替换的字符串。它被视为逐字字符串,不被解释为常规字符串 表达。只会替换第一个匹配项。

【讨论】:

  • 感谢您的详细解释! :)
  • 为空白填充添加了trim 选项
猜你喜欢
  • 2017-01-22
  • 1970-01-01
  • 2022-11-10
  • 2019-11-27
  • 2013-05-18
  • 1970-01-01
  • 2012-07-23
  • 2014-05-31
  • 1970-01-01
相关资源
最近更新 更多