小编Sho*_*hoe的帖子

RegExp取代每秒星号?

我有一个字符串,例如:

"The red letters in the following words are suffixes: beauti*ful*, speech*less* and invinc*ible*."
Run Code Online (Sandbox Code Playgroud)

我想**<span class='red'>第二个替换第一个和第二个</span>.我可以在for循环中执行此操作,但想知道如何使用RegExp执行此操作.

javascript regex

3
推荐指数
1
解决办法
420
查看次数

如何动态地将带有数组键的对象添加到对象文字?

我有一个现有的对象文字:

var words=[
    {
"word":"callous",
"definition":"uncaring",
"examples": [
   {"sentence":"How could you be so callous?"},
   {"sentence":"What a callous thing to say!"},
   {"sentence":"That showed a callous disregard for the consequences."}
]
}
Run Code Online (Sandbox Code Playgroud)

];

我正在尝试动态添加更多对象,如下所示:

var obj={};
obj.word="nextword";
obj.definition ="nextword definition";
obj.examples= ???;
for (var i = 0; i < nextwordSentencesArray.length; i++) {
 ??? obj.examples.sentence.push(nextwordSentencesArray[i]);
}
words.push(obj);
Run Code Online (Sandbox Code Playgroud)

我曾在-???-尝试过各种选项,但没有任何效果。感激之至。

javascript arrays object-literal

1
推荐指数
1
解决办法
45
查看次数

标签 统计

javascript ×2

arrays ×1

object-literal ×1

regex ×1