我有这样的字符串:var name = "\n\n\t\n\n\t\t\n\t\t\tName\n\t\t\n\n\t\t(send picture)\n\t\n"
我想得到第一个单词(在此示例中->名称)。
我已经尝试过正则表达式,但是无法编写正确的表达式。
我的尝试:
`first_name = name.match(/^([\w\-]+)/)`
最佳答案
使用\w+
匹配带下划线的字母数字字符
var name = "\n\n\t\n\n\t\t\n\t\t\tName\n\t\t\n\n\t\t(send picture)\n\t\n";
var matches = name.match(/\w+/g);
if (matches) {
console.log("First word - ", matches[0]);
}
演示版