Solution 1 :

You can use a simple regex like:

var serverData = "12345 - Hello How are you!";
var serverData_1 = "12 - Washington";
var otherData = "965 - FileData";


console.log(/d+/.exec(serverData)[0]);
console.log(/d+/.exec(serverData_1)[0]);
console.log(/d+/.exec(otherData)[0]);

Solution 2 :

If you always have the ” – “, you can use the split command:

var server1 = serverData.split(" - ", 1);

Solution 3 :

You can use regular expressions.

Example

var serverData = "12345 - Hello How are you!"
var serverData_1 = "12 - Washington"
var otherData = "965 - FileData"

var server1 = /d+/.exec(serverData)[0]; //result "12345"
var server2 = /d+/.exec(serverData_1)[0]; //result "12"
var server3 = /d+/.exec(otherData)[0]; //result "965"

Solution 4 :

Well if the struktur of the String always in this form “NUMBER – TEXT”

you can use

var server1 = serverData.split(' - ')[0];
var server2 = serverData_1.split(' - ')[0];
var server3 = otherData.split(' - ')[0];

But if you need it more generic

var server1 = /d+/.exec(serverData)[0];
var server2 = /d+/.exec(serverData_1)[0];
var server3 = /d+/.exec(otherData)[0];

Solution 5 :

var serverData = "12345 - Hello How are you!";
var serverData_1 = "12 - Washington";
var otherData = "965 - FileData";

function getNumbers(txt) {
   var numb = txt.match(/d/g);
   numb = numb.join("");
   return numb;
}

console.log(getNumbers(serverData));
console.log(getNumbers(serverData_1));
console.log(getNumbers(otherData));

For more ways how to do it, please refer to: Extract a number from a string (JavaScript)

Problem :

Original Text

var serverData = "12345 - Hello How are you!"
var serverData_1 = "12 - Washington"
var otherData = "965 - FileData"

How to extract only numbers from these 3 strings.
So that I can get:

var server1 = "12345" from var serverData
var server2 = "12" from var serverData_1
var server3 = "965" from var otherData

By