sadssdsada sadssdsada - 1 year ago 36
HTML Question

JavaScript - split groups of strings between ( ) and put them inside two separated inputs

I have this line:

first (word1-word2-word3) second (any1-any2-any3)

Lets say I have a textarea and two inputs (input1 and input2).

When I paste the line above inside the textarea, and by using JavaScript, I want:


to go to the first input (input1) and for the other part;


to go to the second input (input2).

Thank you.

Answer Source

You could use a regular expression and take the two matching groups.

function split(v) {
    var m = v.match(/^.*\((.*)\).*\((.*)\)$/);
    document.getElementById('input1').value = m[1];
    document.getElementById('input2').value = m[2];
<textarea onchange="split(this.value)"></textarea><br>
<input type="text" id="input1"><br>
<input type="text" id="input2">

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