gpt4 book ai didi

javascript - 如何获取字符串中的第一个单词

转载 作者:行者123 更新时间:2023-11-27 23:38:24 27 4
gpt4 key购买 nike

网络上的答案与其他编程语言相关。我已经进行了很多搜索,但似乎没有一个起作用。我正在寻找每个标题。

var str = ["bob, b", "the, d", "builder, e", "can", "he", "fix", "it" ]
str.match(/^(\w+)/) // Uncaught TypeError: str.match is not a function

我尝试查看this但是......我仍在学习,但我没有正确使用它。

如何仅返回 bob 而不是 bob, b

最佳答案

JS Fiddle

var str = ["bob, b", "the, d", "builder, e", "can", "he", "fix", "it" ];
for(var i=0; i < str.length; ++i){
console.log(str[i].match('[a-zA-Z]+'));
}

关于javascript - 如何获取字符串中的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924620/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com