Reputation: 51
As below example I want to get tokens from string using typescript:
var str = "Hello $FirstName $LastName, Your account with number $AccountNumber will activate soon".
Expected result :
An array: ["$FirstName","$LastName","$AccountNumber"]
Or comma separated string: var commaSepData = "$FirstName,$LastName,$AccountNumber";
Upvotes: 0
Views: 1231
Reputation: 8491
Another way is to use a regexp:
const str = "Hello $FirstName $LastName, Your account with number $AccountNumber will activate soon";
const groups = str.match(/(\$\w+)/g);
console.log(groups);
Upvotes: 2
Reputation: 1227
Use
var str = "Hello $FirstName $LastName, Your account with number $AccountNumber will activate soon";
res = str.split(' ').filter( chunk => chunk.includes('$'))
//res = ["$FirstName", "$LastName,", "$AccountNumber"]
Upvotes: 0