在 javascript 中使用正则表达式和 replace() 替换数组元素
Replacing array elements using regex and replace() in javascript
我对 JavaScript 有点陌生,我正在尝试使用与字符串匹配的正则表达式替换数组元素,这是我试过的代码
<button onclick="myFunction()">ClickHere</button>
<p id="demo"></p>
<script>
function myFunction() {
var abc = ["deno", "eno","pqr","lenovo"];
var i,text;
for(i = 0; i < abc.length; i++) {
text += abc[i].replace(/no/i, "po");
document.getElementById("demo").innerHTML = text;
}
}
</script>
我想用 "po" 替换数组元素字符串中遇到 "no" 的数组元素。
这是我所期望的:
abc["depo","epo","pqr","lepovo"]
您可以对每个元素执行此操作:
for(var i=0; i < abc.length; i++) {
abc[i] = abc[i].replace('no', 'po');
}
或使用一行
abc = abc.map(function(x){return x.replace('no', 'po');});
或使用一行 "arrow functions":
abc = abc.map(x => x.replace('no', 'po'));
更改数组后,可以使用以下方法将其转换为字符串:
var text = 'abc[';
for ( var i = 0 ; i < abc.length ; i++ ) {
text+='\"'+abc[i]+'\"';
if ( i != abc.length - 1) {
text+=',';
}
}
text += ']';
测试:
function myFunction() {
var abc = ["deno", "eno","pqr","lenovo"];
abc = abc.map(x => x.replace('no', 'po')); // see other 2 alternatives above
var text = 'abc[';
for ( var i = 0 ; i < abc.length ; i++ ) {
text+='\"'+abc[i]+'\"';
if ( i != abc.length - 1) {
text+=',';
}
}
text += ']';
document.getElementById("demo").innerHTML = text;
}
<button onclick="myFunction()">ClickHere</button>
<p id="demo"></p>
var i, text;
for(i = 0; i < abc.length, i++) {
text += abc[i].replace("no", "po");
}
console.log(text);
您的代码需要进行三处更改:
- 初始化文本为空 string.Because 默认情况下未定义。
- 将 abc[i].length 更改为 abc.length。
for循环中abc[i].length后的逗号替换为分号
var abc = ["deno", "eno","pqr","lenovo"];
var i;
var text = "";
for(i = 0; i < abc.length; i++) {
text += abc[i].replace("no", "po");
}
我对 JavaScript 有点陌生,我正在尝试使用与字符串匹配的正则表达式替换数组元素,这是我试过的代码
<button onclick="myFunction()">ClickHere</button>
<p id="demo"></p>
<script>
function myFunction() {
var abc = ["deno", "eno","pqr","lenovo"];
var i,text;
for(i = 0; i < abc.length; i++) {
text += abc[i].replace(/no/i, "po");
document.getElementById("demo").innerHTML = text;
}
}
</script>
我想用 "po" 替换数组元素字符串中遇到 "no" 的数组元素。
这是我所期望的:
abc["depo","epo","pqr","lepovo"]
您可以对每个元素执行此操作:
for(var i=0; i < abc.length; i++) {
abc[i] = abc[i].replace('no', 'po');
}
或使用一行
abc = abc.map(function(x){return x.replace('no', 'po');});
或使用一行 "arrow functions":
abc = abc.map(x => x.replace('no', 'po'));
更改数组后,可以使用以下方法将其转换为字符串:
var text = 'abc[';
for ( var i = 0 ; i < abc.length ; i++ ) {
text+='\"'+abc[i]+'\"';
if ( i != abc.length - 1) {
text+=',';
}
}
text += ']';
测试:
function myFunction() {
var abc = ["deno", "eno","pqr","lenovo"];
abc = abc.map(x => x.replace('no', 'po')); // see other 2 alternatives above
var text = 'abc[';
for ( var i = 0 ; i < abc.length ; i++ ) {
text+='\"'+abc[i]+'\"';
if ( i != abc.length - 1) {
text+=',';
}
}
text += ']';
document.getElementById("demo").innerHTML = text;
}
<button onclick="myFunction()">ClickHere</button>
<p id="demo"></p>
var i, text;
for(i = 0; i < abc.length, i++) {
text += abc[i].replace("no", "po");
}
console.log(text);
您的代码需要进行三处更改:
- 初始化文本为空 string.Because 默认情况下未定义。
- 将 abc[i].length 更改为 abc.length。
for循环中abc[i].length后的逗号替换为分号
var abc = ["deno", "eno","pqr","lenovo"]; var i; var text = ""; for(i = 0; i < abc.length; i++) { text += abc[i].replace("no", "po"); }