我有一个带有HTML标签和自由文本的字符串
let paragraph = ` <p class="card-text" style="font-weight: 100">This is
image 1. </p>`
我正在尝试将标点符号和空格分开,只显示以下内容
[p, class, card-text, style, font-weight, 100, this, is image 1, p]
我尝试使用以下方法
let elements = paragraph.split(" ")
但是得到了
["", "<p", "class="card-text"", "style="font-weight:", "100">This", "is", "↵", "", "", "", "", "", "image", "1.", "</p>"]
除了拆分,还有其他方法吗?
谢谢
答案 0 :(得分:2)
如果要匹配单词,可以使用匹配单词字符的正则表达式:
let paragraph = `<p class="card-text" style="font-weight: 100">This is
image 1. </p>`
let result = paragraph.match(/\w+/g)
console.log(result)