在HTML文本框中查找某些子字符串,然后用其他字符串替换它们
Look through a HTML textbox for certain substrings, then replace them with other strings
我正在为一位在工作中填写表格的家庭成员制作一款应用程序。这需要她一次填写很长的文本,这会变得重复和烦人。我有过写短代码名的想法(比如"GDS"代表"良好的国内标准")。我希望程序在文本框中搜索某些(定义的)代码名,并用它们的"正确含义"替换它们。到目前为止,我有这个:
<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
。
您可以使用字符串对象的replace方法: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/
根据您的需要,我会在缩写形式和扩展形式之间创建一个映射,并使用String.replace()
替换找到的缩写。
有了这个映射,您还可以轻松地添加新的映射,也可以轻松地让用户保存自己的映射。(使用LocalStorage、IndexedDB或类似软件)
var mapping = {};
mapping['GDS'] = 'Good Domestic Standard';
mapping['PHP'] = 'Personal Home Page';
mapping['JS'] = 'JavaScript';
document.getElementById('replace').addEventListener('click', function(evt) {
var newString = (function(map, oldString) {
Object.keys(map).forEach(function(key) {
oldString = oldString.replace(new RegExp('''b' + key + '''b', 'g'), map[key]);
});
return oldString;
}(mapping, document.getElementById('input').value));
output.value = newString;
});
textarea {
width: 600px;
height: 120px;
border: 3px solid #cccccc;
padding: 5px;
}
<button id="replace">Replace</button>
<p>
Original Text:
<textarea id="input">GDS in the beginning, inthePHPmiddle, but PHP works. Also, JS. GDS for good measure.</textarea>
</p>
<p>
New Text:
<textarea id="output"></textarea>
</p>
您将使用replace
方法:
boxText = boxText.replace(/'bGDS'b/g, 'Good Domestic Standard');
'b
匹配一个单词边界,因此它计算独立的代码名称,而不是作为另一个单词的一部分。
要替换多个字符串,可以使用与任何字符串匹配的模式,并使用回调函数选择替换:
boxText = boxText.replace(/'b(GDS|ADS|PDS)'b/g, function(m){
switch (m) {
case 'GDS': return 'Good Domestic Standard';
case 'ADS': return 'Average Domestic Standard';
case 'PDS': return 'Poor Domestic Standard';
}
});
演示:
var boxText = "GDS, ADS, PDS";
boxText = boxText.replace(/'b(GDS|ADS|PDS)'b/g, function(m){
switch (m) {
case 'GDS': return 'Good Domestic Standard';
case 'ADS': return 'Average Domestic Standard';
case 'PDS': return 'Poor Domestic Standard';
}
});
// show result in Stackoverflow snippet
document.write(boxText);
没有测试,但这应该可以做到:
function getBoxText() {
// store strings in object to have them easily maintainable
var strings = {
'GDS' : 'Good Domestic Standard',
'GTA' : 'Grand Theft Auto',
'FUBAR' : 'fucked up beyond all recognition'
};
var boxText = document.getElementById("textBox").value;
for (var shortcode in strings) {
boxText = boxText.replace(shortcode,strings[shortcode]);
}
document.getElementById("bottomText").innerHTML = boxText;
}
相关文章:
- 用有限的字符集压缩字符串,然后在JS中解压缩
- 我如何从字符串中选出第一个单词的第一个字母,然后再选出第二个单词
- 在C#中压缩字符串,然后在JavaScript中解压缩
- 如何拆分一个字符串,然后将它们与另一个字符串匹配
- Json 字符串化然后从 URL 解析
- 在元素中搜索字符串,然后将其放入条件语句中
- 用我的名字创建一个变量.然后提醒字符串“hello-from”加上我创建的变量
- 在HTML文本框中查找某些子字符串,然后用其他字符串替换它们
- 如何使用ajax将字符串传递给python程序,然后将字符串发回
- 我想使用replace函数扫描json对象,然后用字符串替换匹配的单词
- node.js将接收到的字符串拆分为不同的变量,然后格式化为json字符串
- 通过regex匹配ahref标记,然后转换为字符串
- 将JSON字符串转换为JavaScript对象,然后创建一个HTML表
- Metor:将html保存为字符串,然后将其转换为DOM节点
- 正则表达式,用于查找字符串的所有出现项,然后查找其后面的下一个空格
- 如何使用Javascript/JQuery检查字符串的形式是否为10 * 10 * 10 * 10,然后进行数学运算
- Javascript:模拟字符串的键入而不格式化,然后使用格式化输出它
- 将文本框中用逗号分隔的员工ID拆分为数组,然后处理这些字符串
- JQuery attr 选择器到字符串,存储为变量,然后传递到另一个 JQuery 选择器
- 加载表单数据到字符串然后到localStorage的最佳方式是什么?