regex to grab url from string [duplicate] - javascript

This question already has answers here:
Regular expression to find URLs within a string
(35 answers)
Closed 7 years ago.
I have this json object
arr[i].text
which returns
check this http://www.newlook.com/shop/womens/dresses/navy-short-sleeve-check-tunic-dress-_320165649
I want to return only the URL with a regex like so:
var urlreg = /(\bhttps?\:\/\/(www)?\.\w+\.\w+(\/[\w\d\-]+)*)/;
match = urlreg.exec(arr[i].text );
but doesn't work, is it something to with it being an object and not a string?

Try: var urlreg = /(https?:\/\/(\w+\.)+\w+(\/[\w\-_]+)+)/\/?
Here is a demo

Related

Is there is a way to replace multiple character with replace function? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
my replace function only replace one character.
My Code example
const string = "What,Yo,Yes"
string.replace(","," ")
Console
"What Yo,Yes"
Something like this:
const string = "What,Yo,Yes";
console.log(string.replace(/,/g," "));
Alternatively, you can use replaceAll, like this:
const string = "What,Yo,Yes";
console.log(string.replaceAll(","," "));

Return everything after phrase, to end of string, excluding phrase [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
I have the following URL
https://website.com?id=XXXVVVCCCHHH
I only want XXXVVVCCCHHH, I've tried the following:
var phrase = 'https://website.com?id=XXXVVVCCCHHH';
var myRegexp = /id=(.*)/;
phrase = myRegexp.exec(phrase);
But this is returning: id=XXXVVVCCCHHH;
How can Ii edit this to only return XXXVVVCCCHHH?
Just use split and take the second element:
var url = "https://website.com?id=XXXVVVCCCHHH";
var part = url.split('=')[1];
console.log(part);

how to detect and get url on string javascript [duplicate]

This question already has answers here:
Javascript: extract URLs from string (inc. querystring) and return array
(5 answers)
Closed 7 years ago.
how to detect and get url on string javascript?
example :
var string = "hei dude, check this link http:://google.com and http:://youtube.com"
how to get result like this from my string :
var result = ["http:://google.com", "http:://youtube.com"]
how do that?
You input has double :: after http. Not sure if it intentional. If it is then use:
var matches = string.match(/\bhttps?::\/\/\S+/gi);
If only one : is needed then use:
var matches = string.match(/\bhttps?:\/\/\S+/gi);
RegEx Demo
const string = "hei dude, check this link http:://google.com and http:://youtube.com"
const matches = string.match(/\bhttp?::\/\/\S+/gi);
console.log(matches);

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

how to get the value(ID) using javascript substring? [duplicate]

This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');

Categories