kather kather - 1 month ago 10
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.

Input

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


Required Output

["Field1","Field2","Field3","Field4"]


Thanks in advance

Answer

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);
console.log(result);

Comments