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

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

Related

Return a subset of String [duplicate]

This question already has answers here:
Last segment of URL with JavaScript
(30 answers)
Closed 3 months ago.
I have this string:
'/api/media-objects/e78c7cfa-e469-4edd-8a87-9517a5b9e5da'
I want to return only the id ('e78c7cfa-e469-4edd-8a87-9517a5b9e5da') after the last '/'. The id changes everytime I make an API call. How can I do that using any String functions?
You could use match() here:
var input = "/api/media-objects/e78c7cfa-e469-4edd-8a87-9517a5b9e5da";
var output = input.match(/[^\/]+$/)[0];
console.log(output);
Another regex option would be to do a replacement:
var input = "/api/media-objects/e78c7cfa-e469-4edd-8a87-9517a5b9e5da";
var output = input.replace(/^.*\//, "");
console.log(output);
You can use String.prototype.split which splits a string based on the given separator:
const path = '/api/media-objects/e78c7cfa-e469-4edd-8a87-9517a5b9e5da'
const parts = path.split('/')
const id = parts[parts.length - 1] // the last chunk
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
You can create a substring from the position of the last / (+ 1 to omit the it) to the end of the string:
str.slice(str.lastIndexOf('/') + 1)
Note: This assumes that the string will always contain a /. If it's possible that it doesn't you have to handle that case.

Replace values from an array in string non case sensitive in javascript [duplicate]

This question already has answers here:
Case insensitive replace all
(7 answers)
Closed 2 years ago.
My code is as follows:
array.forEach(el => {
string = string.replace(el, `censored`);
});
array : my array of words that I want to censor.
string : the string that the words need censoring.
My issue is that this process is quite slow and also if the word in my string is written using capitals, it's getting missed.
Any ideas how should I solve this issue?
Thank you.
maybe you can use regex
let array = ['mate']
let string = 'Hello Mate, how are you mate?'
let re = new RegExp(array.join("|"),"gi");
let str = string.replace(re, 'censored');
output:
"Hello censored, how are you censored?"

javascript Regular Expressions [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
I have the following url
http://www.test.info/?id=50&size=40
How do I get the value of the url parameter with regular expressions in javascript . i need the size value and also need the url without &?
only
http://www.test.info/?id=50
Thanks
Consider using split instead of a regex:
var splitted = 'http://www.test.info/?id=50&size=40'.split('&');
var urlWithoutAmpersand = splitted[0];
// now urlWithoutAmpersand => 'http://www.test.info/?id=50'
var sizeValue = splitted[1].split('=')[1] * 1;
// now sizeValue => 40
Just use this as your regex
size.*?(?=&|$)
here is some code you can use
var re = /size.*?(?=&|$)/g;
var myArray = url.match(re);
console.log(myArray);
you also can do it like this:
var re = new RegExp("size.*?(?=&|$)", "g");
Here is a regex pattern you could use.
^(.+)&size=(\d+)
The first group will be the url up to right before the '&' sign. The second group will be the value of the size parameter. This assumes id always comes before size, and that there are only two parameters: id and size.

regex to grab url from string [duplicate]

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

How to get string follow url use javascript? [duplicate]

This question already has answers here:
JavaScript query string [closed]
(15 answers)
Closed 7 years ago.
I have a url
http://localhost:8162/UI/UsersDetail.aspx?id_temp=U0001
I want to get string use javascript
?id_temp=U0001
Thank guys.
If this isn't the location of the page, you may use
var str = url.match(/\?.*$/)[0];
If this url is the current one of your page, use
var str = location.search;
You can use regex:
url.match(/\?(\w+=\w+)/)[0];
/ : Delimiter of regex
\? : Matches ? need to escape using \
\w+: Matches all alphanumeric characters and _
= : Matches =

Categories