我有一个如下所示的字符串
var indicator = -65(www.anyweb.com)
数字-65也可以是任何数字。如何在javascript中单独取出网址?
答案 0 :(得分:2)
您需要在'('和之前')'
之后提取字符串var str = "-65(www.anyweb.com)";
str = str.substring(str.lastIndexOf("(")+1,str.lastIndexOf(")"));
答案 1 :(得分:1)
You can use this example for string operations
var data = "-65(www.anyweb.com)";
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')')); console.log("URL :: ",url);
答案 2 :(得分:0)
var domain = /\((.*?)\)/.exec("-65(www.anyweb.com)")[1];
console.log(domain);

上面的正则表达式将创建一个包含括号内任何内容的组。
答案 3 :(得分:0)
您可以使用一些简单的字符串操作:
var str = "-65(www.anyweb.com)";
var url = "N/A";
// Find indices of open and close parentheses
var open = str.indexOf("(");
var close = str.lastIndexOf(")");
// If they were found then extract the URL from the string
if (open !== -1 && close !== -1) {
url = str.substring(open + 1, close);
}
console.log(url);
如果您更倾向于使用正则表达式,那么应该这样做:
var str = "-65(www.anyweb.com)";
var regex = /\((.*?)\)/; // Capture URL inside parentheses
var result = regex.exec(str); // Execute the regex against the string
var url = "N/A";
// If the URL was matched then assign it to the variable
if (result[1] !== undefined) {
url = result[1];
}
console.log(url);
您也可以简单地替换您不想要的东西:
var str = "-65(www.anyweb.com)";
str = str.replace(/^.*\(/, ""); // Remove everything before URL
str = str.replace(/\).*$/, ""); // Remove everything after URL
console.log(str);
答案 4 :(得分:0)
Example 1 :
var data = "-65(www.anyweb.com)";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
Example 2 :
var data = "-65";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
Example 3 :
var data = "-65(www.anyweb.com)6764872";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);