gpt4 book ai didi

javascript - 通过 HTML 文本框查找某些子字符串,然后用其他字符串替换它们

转载 作者:行者123 更新时间:2023-11-30 10:06:36 25 4
gpt4 key购买 nike

我正在为填写表格作为其工作的一部分的家庭成员制作一个应用程序。这需要她一次填写很长的文本,这变得重复且烦人。我有写短代号的想法(比如“GDS”代表“Good Domestic Standard”)。我希望程序查看文本框并搜索某些(定义的)代号并将它们替换为它们的“正确含义”。到目前为止,我有这个:

<html>

<style type="text/css">
h1.title
{
font-size: 80px;
font-family: "Verdana";
font-weight: bolder;
}

body
{
background: rgb(200,400,200);
}

p.main
{
font-size: 31px;
font-family: "Verdana";
font-weight: bold;
text-align: left;
}
textarea
{
width: 1000px;
height: 600px;
}
</style>

<script type="text/javascript">
function getBoxText()
{
var boxText = document.getElementById("textBox").value // Get what's in the TextBox.
document.getElementById("bottomText").innerHTML = boxText
}
</script>

<head>
<h1 align="middle" class="title">Test Page for BoxScan</h1>
</head>

<body>
<p class="main">Welcome to BoxScan Test Page!</p>
<textarea id="textBox">Input your text here!</textarea>
<br>
<button onclick="getBoxText()">Submit</button>
<br>
<h1 id="bottomText">Blank</h1>
</body>


</html>

显然,这一点是有问题的部分:

var boxText = document.getElementById("textBox").value // Get what's in the TextBox.
document.getElementById("bottomText").innerHTML = boxText

这行得通,但现在我该如何查看并替换我想要的字符串?以GDS为例,意思是Good Domestic Standard

最佳答案

您可以使用字符串对象的替换方法:http://www.w3schools.com/jsref/jsref_replace.asp

您必须使用正则表达式来替换所有出现的地方。

var boxText = document.getElementById("textBox").value // Get what's in the TextBox.

var newBoxText = boxText.replace(/Initial/g, "Updated");

document.getElementById("bottomText").innerHTML = newBoxText

http://jsfiddle.net/rny94z4j/

关于javascript - 通过 HTML 文本框查找某些子字符串,然后用其他字符串替换它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797384/

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