Currently I have a regular expression that will find all URL's within a block of html. It looks like this:
elementHTML.match(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/ig);
When a URL is detected it is replaced with something that looks like:
<div data="URL_THAT_WAS_DETECTED">Information about that url</div>
The data attribute is custom added.
How can I continue to look for URL's without picking up the previously detected URL?
Ideally, I would like either ignore URL's that are in quotes or possibly html tags but I'm open to suggestions.
Any help is greatly appreciated, Thanks!
This regex will do it:
/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+(?![^\s]*?")([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/ig
I've added a negative lookahead for double quotes " characters.
Live Demo
Related
How come the following 'a' tag:
Redirects to the following wrong link?
(the correct link is 'https://www.ft.com/content/336e7f52-4189-11e8-93cf-67ac3a6482fd', but the url is redirected to 'file:///home/vincent/wintergreen/bokehIB/%E2%80%9Dhttps://www.ft.com/content/336e7f52-4189-11e8-93cf-67ac3a6482fd%E2%80%9D')
What could be the possible reason for that?
The full code describing the full creation of that page is here.
However, my code is python and bokeh package which is a bit niche, and I think that my problem is more of a javascript general thing - hence the reduced simple question.
Your anchor tag is Link here instead of Link here .
Notice that you have double double-quotes (") there.
You have " symbols surrounding your url, because it does not begin with a slash, it is considered relative url. try to remove these.
I am using Html, Jquery and Javascript. I am using linkify javascript library and is working fine. But if there is no space before the URL then it does not work. How to solve this? I have below URL along with text.
Please click the linkhttp://www.asdfgh.com
Please see there is no space before the URL. How to linkify the URL here?Can it be done using Javascript?
don't Know linkify. Maybe there is an option for setting individual patterns.
Otherways you could search the text for this string and replace it, before linkify do its work. But its dirty.
yourtext=yourtext.replace("linkhttp://","link http://");
Let's say the structure of the page URL is http://string.static.com/*
string may vary but will only consist of any combination of alphanumeric characters and dashes.
static.com is self-explanatory.
* could be anyting containing letters, numbers, /,:,? and similar characters, so basically there can be directories and parameters as well.
What I'm trying to achieve is this:
The bookmarklet should replace everyting after .com/... so the url becomes http://string.static.com/archive
I've created two (very similar) JS bookmarklets so far but I can't wrap my head around this one. It should be fairly simple but I haven't had any luck. Thanks in advance.
I am pretty new to this so please take it easy on me.
I have a javascript bookmark that I use to look up tracking number for parcels on Purolators website (Canadian shipping company). What I want it to do is to take the input (tracking number) and remove any spaces in it before opening the URL. The tracking site is stupid and uses spaces as delimiters for new tracking numbers.
Also, as a bonus, can this be made to open in a new tab?
javascript:var%20trackID%20=%20escape(prompt('Enter%20Tracking%20#'));window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='%20+%20trackID;
I have tried adding trackID%20=%20trackID.replace(/\s+/g,%20''); before the window.location but no luck.
Any ideas?
You don't need to use escape in this scenario. This is actually replacing your spaces with %20 special characters which is why your replace wasn't working
Check http://www.w3schools.com/jsref/jsref_escape.asp for more info.
Your corrected code should be something like
javascript:var trackID=prompt('Enter%20Tracking%20#').replace(/ /g,"");window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='+trackID;
I have a few strings and I would like to insert some line breaks into them at certain points.
I figured out a few of the logistics but as a whole I can't seem to crack this problem, probably because I have limited experience with regex.
Basically I have a long string of XML tags that is all on one line. I want to add line breaks at certain points to get the data more formatted and looking nice. I am using CodeMirror to display this data on a webpage but for some reason its all on line #1.
So I need to go from something like this:
<Sample><Name></Name><PhoneNumber><AreaCode></AreaCode><Number></Number></PhoneNumber></Sample>
To something like this:
<Sample>
<Name></Name>
<PhoneNumber>
<AreaCode></AreaCode>
<Number></Number>
</PhoneNumber>
</Sample>
CodeMirror will take care of the rest of the formatting all I need to do is insert the line breaks in the right spot using regex or a loop of some sort. The Tags will or can change so I am guessing regex has to be used.
I have had success inserting line breaks with \n and 
 but can't seem to get regex to detect the proper locations.
Any help would be greatly appreciated. Thanks.
UPDATE
I overlooked this but the brackets are in fact being sent as < and >
So example tag would look like:
<PhoneNumber>
or
</PhoneNumber>
So basically need to insert a \n after every > that is a closing tag or a beginning tag that contains children tags.
There be dragons here.
I'd like to point you to a very similar question answered awhile ago that does a good job of explaining why you should NOT try to parse XML yourself unless you REALLY know what you're doing.
Use an XML deserializer if you want to get nice line breaks and that sort of thing.
Try this regex pattern:
>\s*<(?!/)
Replacement string : >\n<
UPDATE:
>\s*<(?!/)