kather kather - 1 year ago 103
Javascript Question

Use regex to get array of values from string in javascript

I am trying to get all the words comes with inside bracket in string using javascript but i am not get clear idea. please help me.


" ( [Field1] + [Field2] ) + ( [Field3] - [Field4] ) "

Required Output


Thanks in advance

Answer Source

You can easily use String.prototype.match() that select specific part of string by regex. Note that the \w+ matches word characters.

var str =  "  (  [Field1]  +  [Field2]  )    +    (  [Field3]  -  [Field4]  )  ";
var result = str.match(/\w+/g);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download