gpt4 book ai didi

JavaScript : replace string at specific position

转载 作者:行者123 更新时间:2023-11-28 13:32:31 26 4
gpt4 key购买 nike

我有一个这样的字符串:

var str = "Hello, it is a <test>12345</test> and it's fun";

我想用“****”替换“12345”。

脚本必须考虑到以下事实:包含 和 之间的字符串永远不会相同。

最佳答案

您可以使用此正则表达式:

str = str.replace(/(<test>).*?(?=<\/test>)/, '$1*****');
//=> "Hello, it is a <test>*****</test> and it's fun"

如果您不想先行,请在两侧使用捕获组:

str = str.replace(/(<test>).*?(<\/test>)/, '$1*****$2');
//=> "Hello, it is a <test>*****</test> and it's fun"

关于JavaScript : replace string at specific position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23897569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com