我正在尝试使用 javascript 从标签中提取字符串:
document.querySelector('.title h2').textContent
但我得到这样的东西(包括双引号):
"(lots of spaces) String stuff (lots of spaces)"
我实际上只需要检索字符串中的文本部分(本例中的字符串内容),留下双引号和空格。
我知道我需要一些正则表达式,但我不知道在那种情况下如何进行。
最佳答案
尝试对字符串使用 trim 方法。
var a = " String stuff ";
console.log(a.trim()); // Prints: "String stuff"
关于Javascript - 只选择字符串中的特定模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086523/