我想从打字稿中的字符串中获取以 $ 开头的单词(标记)

I want to get words (token) from string in typescripts starts with $

如下例,我想使用打字稿从字符串中获取标记:

var str = "Hello $FirstName $LastName, Your account with number $AccountNumber will activate soon".

预期结果:

数组:["$FirstName","$LastName","$AccountNumber"]

或逗号分隔的字符串:var commaSepData = "$FirstName,$LastName,$AccountNumber";

使用

var str = "Hello $FirstName $LastName, Your account with number $AccountNumber will activate soon";

res = str.split(' ').filter( chunk => chunk.includes('$'))

//res = ["$FirstName", "$LastName,", "$AccountNumber"]

另一种方法是使用正则表达式:

const str = "Hello $FirstName $LastName, Your account with number $AccountNumber will activate soon";
const groups = str.match(/($\w+)/g);
console.log(groups);