i have written bellow code in javascript
function reg()
{
var a="ec2-54-234-174-228.compute-1.amazonaws.com";
var r = a.match(/\-[0-9]*/g);
alert(r);
}
i got output like -54,-234,-174,-228,-1
but i need to extract only 54-234-174-228
IP address from variable a.
Try this:
function reg()
{
var a="ec2-54-234-174-228.compute-1.amazonaws.com";
var r = a.match(/\-[0-9-]*/g);
alert(r[0].substring(1,r[0].length));
}
a.match(/\\-[0-9-]*/g);
will return [-54-234-174-228,-1]
. Getting the first element and removing -
from the beginning you get what your IP. You can also add this:
alert(r[0].substring(1,r[0].length).replace(/-/g, '.'));
to return it in IP shape: 54.234.174.228
Try this regexp: /[0-9]{1,3}(-[0-9]{1,3}){3}(?=\\.)/
It matches a series of 4 numbers between 1 and 3 digits separated by -, which must be followed by a dot.
get the index of first "." then slice
function reg()
{
var a="ec2-54-234-174-228.compute-1.amazonaws.com";
var r = a.indexOf(".");
alert(a.slice(0,r));
}
"ec2-54-234-174-228.compute-1.amazonaws.com".split('.')[0].split('-').slice(1,5).join('.')
就您而言,您可以简单地使用以下模式:
var r = a.match(/([^a-z][0-9]+\-[0-9]+\-[0-9]+\-[0-9]+)/g);
您可以使用此正则表达式
"(\\d{2}-\\d{3}-\\d{3}-\\d{3})+"
One more example:
function reg() {
var a="ec2-54-234-174-228.compute-1.amazonaws.com";
var re = /-(\d+)/ig
var arr = [];
while(digit = re.exec(a)) arr.push(digit[1]);
arr = arr.slice(0,4);
alert(arr)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.