gpt4 book ai didi

javascript - 将我的页面卡在某个字符串上而没有错误

转载 作者:行者123 更新时间:2023-11-30 07:20:50 24 4
gpt4 key购买 nike

出于某种原因,这使我的页面卡住并且不显示 alert

我正在尝试将某些文本中的 " 替换为 \"

示例参数

搜索 = "

替换=\"

Text = "hello world"

注意文本"hello world" 不是 hello world

预期的输出(文本)应该是\"hello world\"

var Search = prompt("What To Search?"); // It will be the sign "
var Replace = prompt("What Sign To Replace?"); // It will be the sign \"
var Text = prompt("Write Text Here");
while(Text.includes(Search))
{
Text=Text.replace(Search,Replace);
} // It's didn't replace all so I did this
alert(Text);

最佳答案

它卡住了,因为你进入了一个无限循环

使用这个

String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};

var Search=prompt("What To Search?"); // It will be the sign "
var Replace=prompt("What Sign To Replace?"); // It will be the sign \"
var Text=prompt("Write Text Here");

Text=Text.replaceAll(Search,Replace);

alert(Text);

现在试试

Text=Text.replaceAll(Search,Replace); 

代替

Text=Text.replace(Search,Replace); 

See working Example here

关于javascript - 将我的页面卡在某个字符串上而没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166758/

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