我正在尝试通过使用javascript从标签中提取字符串:
document.querySelector('.title h2').textContent
但是我得到这样的东西(包括双引号):



而且我实际上只需要检索字符串中的文本部分(在此示例中为字符串),留在双引号和空格之后。

我知道我需要一些正则表达式,但是在这种情况下我不知道该如何进行。

最佳答案

尝试对字符串使用修剪方法。

var a = "    String stuff    ";
console.log(a.trim());       // Prints: "String stuff"

Here is the documentation

10-06 04:34
查看更多