Javascript,替换类里面句子中的第一个单词

标签 javascript html

我想帮助您了解如何用 Marty 替换名称类中的名字。我只想使用 Javascript 而不是 jQuery。

所以这应该会导致“Marty Clock”。

<span class="name">Brad Clock</span>

这就是我现在所拥有的。

const article = document.querySelector('article');
const name = article.querySelectorAll('.name');


for (var i = 0; i < name.length; i++) {
 const text = name[i].textContent.split(" ").replace([0], ' ').join("Marty")

 console.log(text);



}
<article>
   
   <p>Does your mom know about tomorrow night? Good morning, Mom. Oh, <span class="name">Marty</span>, I almost forgot, <span class="name">Jennifer Parker</span> called. He's alright. Hey c'mon, I had to change, you think I'm going back in that zoot suit? The <a href="https://giphy.com/gifs/film-back-to-the-future-anniversary2015-Y1aL1fxTEbhQs" art="Spooky young man">old man</a> really came through it worked. <a href="https://giphy.com/gifs/FrVlu71LVoVXy" alt="I guess you guys aren't ready for that yet">I have to tell you about the future</a>.</p>
   
   <p>He's absolutely right, <span class="name">Marty</span>. the last thing you need is headaches. Well, I guess that's everything. Please note that <span class="name">Einstein's</span> clock is in complete synchronization with my control watch. Good evening, I'm Doctor <span class="name">Emmet Brown</span>, I'm standing here on the parking lot of- What did you sleep in your clothes again last night.</p>
   <p>Precisely. <span class="name">George</span>,  Hey beat it, spook, this don't concern you. <a href="https://giphy.com/gifs/back-to-the-future-behind-scenes-CdAh3Sh0Mvtdu" alt="I'm flying!">Bet your ass it works.</a> Here you go, lady. There's a quarter.</p>
  </article>

最佳答案

正则表达式 /[^\s']*/ 匹配 空格' 字符之前的所有内容。为了方便起见,使用了 Array.prototype.forEach()。

var names = document.querySelectorAll('.name');

names.forEach((name) => {

  name.innerText = name.innerText.replace(/[^\s]*/, 'Marty')

})
<article>
   
   <p>Does your mom know about tomorrow night? Good morning, Mom. Oh, <span class="name">Marty</span>, I almost forgot, <span class="name">Jennifer Parker</span> called. He's alright. Hey c'mon, I had to change, you think I'm going back in that zoot suit? The <a href="https://giphy.com/gifs/film-back-to-the-future-anniversary2015-Y1aL1fxTEbhQs" art="Spooky young man">old man</a> really came through it worked. <a href="https://giphy.com/gifs/FrVlu71LVoVXy" alt="I guess you guys aren't ready for that yet">I have to tell you about the future</a>.</p>
   
   <p>He's absolutely right, <span class="name">Marty</span>. the last thing you need is headaches. Well, I guess that's everything. Please note that <span class="name">Einstein's</span> clock is in complete synchronization with my control watch. Good evening, I'm Doctor <span class="name">Emmet Brown</span>, I'm standing here on the parking lot of- What did you sleep in your clothes again last night.</p>
   <p>Precisely. <span class="name">George</span>,  Hey beat it, spook, this don't concern you. <a href="https://giphy.com/gifs/back-to-the-future-behind-scenes-CdAh3Sh0Mvtdu" alt="I'm flying!">Bet your ass it works.</a> Here you go, lady. There's a quarter.</p>
  </article>

关于Javascript,替换类里面句子中的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47199953/

相关文章:

javascript - 许多 setIntervals 与每秒许多选择

java - 图像未在 TextView Html.fromHtml 方法中加载

javascript - 在 HTML 页面中包含数据的最佳方式是什么?

html - 文本区域标签垂直对齐 : middle

html - Bootstrap css navbar-inverse 下拉列表如何更改复选框标签文本的颜色

javascript - Express.js : Convert req. body 变成 POST 编码的字符串

javascript - 在我的网站上嵌入的 Flash 对象是否会成为 Javascript 键事件的问题?

javascript - AngularJs ng-repeat 循环遍历 2 个数组

html - 我的 table 不允许我的复选框并排放置。

javascript - asp.net bootstrap 在回发事件后保持当前事件选项卡