1

I have a string like below

var indicator = -65(www.anyweb.com)

the number -65 can be any number too. How can I take out only the web url separately in javascript?

3
  • I have no idea what -65(www.anyweb.com) means in JavaScript? Is this an example of what you're trying to do? Commented May 31, 2016 at 2:30
  • Am I correct in assuming the variable is a string? Commented May 31, 2016 at 2:31
  • I'm pulling this whole string from backend. What I need to do was to take out only the url and assign as hyperlink to value -65(for example) Commented May 31, 2016 at 2:32

5 Answers 5

2

You need to extract the string after '(' and before ')'

var str = "-65(www.anyweb.com)";
str = str.substring(str.lastIndexOf("(")+1,str.lastIndexOf(")"));
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

When there is no url in data such as only -65, this returns -6 which is not correct for my case
0

var domain = /\((.*?)\)/.exec("-65(www.anyweb.com)")[1];
console.log(domain);


The regex above will create a group with anything that's inside parenthesis.

Comments

0

You can use some simple string operations:

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);

If you are more inclined to use regular expressions then this should do it:

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);

You can also simply replace the stuff that you do not want:

var str = "-65(www.anyweb.com)";
str = str.replace(/^.*\(/, ""); // Remove everything before URL
str = str.replace(/\).*$/, "");  // Remove everything after URL
console.log(str);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.