473,799 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp: Problems with matching a(ny) URI

I need to be able to detect URIs in some text and after this replace
dem with HTML-anchors, that is

http://www.tempuri.org/page.html

should be replaced with

<a href="http://www.tempuri.org/page.html">http ://www.tempuri.org/page.html</a>

I have made the following code:

re = new RegExp('(((http |https|ftp):\/\/)?\w+[.\w]+([^\w]*[\s]+))');
str = document.forms[0].longtext.value ; //textarea with text to replace
newstr = str.replace(re, '<a href="$1">$1</a>');
document.write( newstr);

However, it doesn't quite work as I would like it to. It seems to only
make a single match, and it seems to ingore leading and trailing
whitespaces.

Can you help me solving this?

I test of the code I have so far can be found at
http://www.stocholm.dk/test/html/regexp.html

Thanks,

:o)

--
Jesper Stocholm - http://stocholm.dk
Copenhagen, Denmark
Jul 20 '05 #1
3 4322

"Jesper Stocholm" <je*****@stocho lm.invalid> schreef in bericht
news:Xn******** *************** *@130.226.1.34. ..
I need to be able to detect URIs in some text and after this replace
dem with HTML-anchors, that is .... I have made the following code:

re = new RegExp('(((http |https|ftp):\/\/)?\w+[.\w]+([^\w]*[\s]+))'); .... However, it doesn't quite work as I would like it to. It seems to only
make a single match, and it seems to ingore leading and trailing
whitespaces.


Try this regexp:

re = /(http|https|ftp )([^ ]+)/ig;

(The i at the end means ignore case, the g means global for multiple
matches)

This doesn't allow spaces in URL's (which isn't allowed anyway).
JW

Jul 20 '05 #2

"Janwillem Borleffs" <jw*@jwbfoto.de mon.nl> schreef in bericht
news:3f******** *************** @news.euronet.n l...

Try this regexp:

re = /(http|https|ftp )([^ ]+)/ig;


Forgot to mention that the replacement should be done as follows:

newstr = str.replace(re, '<a href="$1$2">$1$ 2</a>');
JW

Jul 20 '05 #3
Jesper Stocholm <je*****@stocho lm.invalid> writes:
I need to be able to detect URIs in some text and after this replace
dem with HTML-anchors, I have made the following code:

re = new RegExp('(((http |https|ftp):\/\/)?\w+[.\w]+([^\w]*[\s]+))'); .... However, it doesn't quite work as I would like it to.


Step back from the technical part and answer this question:
What is an URL?
or more precisely:
What will you accept as an URL?
(The formal definition is in RFC2396
<URL:http://rfc.sunsite.dk/rfc/rfc2396.html>)

If you can answer it, in detail, then I bet it is easier to make a
regular expression to match it (or get help doing it, because then
the job is precisely specified).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2566
by: Patryk Konieczka | last post by:
Hello Here's the thing I have a database edited by some company workers editing descriptions of books in the sotre , unfortunately these workers do not have the habit of inserting a space character after word-ending dot, this is why i use the function: function getCorrect($txt) { $txt=str_replace(",",", ",$txt); $txt=str_replace(".",". ",$txt);
1
1516
by: joh12005 | last post by:
Hello, here is a trouble that i had, i would like to resolve it with python, even if i still have no clue on how to do it. i had many small "text" files, so to speed up processes on them, i used to copy them inside a huge one adding some king of xml separator : <file name="...">
6
7204
by: Rizyak | last post by:
******************** alt.php.sql,comp databases.ms-sqlserver microsoft.public.sqlserver.programming *********************************** Why doesn't this work: SELECT * FROM 'Events'
3
9176
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the RegExp object to be non-greedy as using the ? quantifier doesn't seem to work. --------- SAMPLE ----------------
3
2696
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null, as every second call to .test() fails. <script> var str = "RegExp (chokes) on this every 2nd time"; var re = /chokes/g;
7
3452
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text, re, 3));
4
2753
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the re module in python, since this one takes the regexp as if were specified this way: '^<widget class=".*"
9
6901
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post by:
With regexps you can search for strings matching it. For example, given the regexp: "foobar\d\d\d". "foobar123" would match. I want to do the reverse, from a regexp generate all strings that could match it. The regexp: "{3}\d{3}" should generate the strings "AAA000", "AAA001", "AAA002" ... "AAB000", "AAB001" ... "ZZZ999". Is this possible to do? Obviously, for some regexps the set of matches is unbounded (a list of everything that...
4
3909
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global' is not an appropriate modifier for the test() function - test() searches the entire string by default. However, I would expect it to degrade gracefully. Instead, I seem to be getting something as follows - using W3Schools handy page at :
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.