Harsh Gajjar
Harsh Gajjar

Reputation: 51

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

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

Answers (2)

Kirill Simonov
Kirill Simonov

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

Arun Mohan
Arun Mohan

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

Related Questions