Home > Mobile >  How to take particular character from a string and store it in a array
How to take particular character from a string and store it in a array

Time:07-20

var element = "Connectorparent3cchild4d" element.match(/\d /g)

previously the code has written to take numeric value from the string

but now var element = "Connectorparentabcchildcd"

is there any way to store the value abc and cd in a array

I need to store the value which is after Connectorparent and child in an array in typescript

CodePudding user response:

You might first remove "Connectorparent" and split on "child" to get both abc and cd in an array

var element =  "Connectorparent3cchild4d"
const arr = element.replace("Connectorparent", "").split("child");
console.log(arr);

CodePudding user response:

Try /Connectorparent(.*)child(.*)/ as yours regex:

var element = "Connectorparent3cchild4d"
const [all, parrent, child] = element.match(/Connectorparent(.*)child(.*)/)

You can read about groups in regex to better understand, or use https://regexr.com/ to explain or build yours regexs.

  • Related