473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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 4306

"Jesper Stocholm" <je*****@stocholm.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.demon.nl> schreef in bericht
news:3f***********************@news.euronet.nl...

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*****@stocholm.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
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...
1
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...
6
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
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...
3
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,...
7
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...
4
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...
9
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...
4
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'...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.