Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下字符串
var str = "Format-ABC-D Type-O"
我想从这个字符串中得到“ABC-D”。我太胖了
str.(/Format-(\w+)/i)[1]
但这只是给了我“ABC”。如何匹配空格?
匹配除空格以外的任何内容,而不仅仅是单词字符:
/Format-(\S+)/i
您可以先将字符串修剪到空格,然后再执行您的操作。所以
str = str.substr(0, str.indexOf(" ")) // str is "Format-ABC-D"