473,320 Members | 2,094 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,320 software developers and data experts.

Is it possible to write reg.expression for such a replace?

Dasty
101 Expert 100+
My problem:

My users will write some text into <textarea> area. All I want to do with javascript is to take that textarea.value and replace all url links there with regular <a href> and then put whole text (with replaced urls) into another div.

Let's show it on example:

User will write sentence:

"This is my site http://www.try.org/see.html and I would like to replace it"

and I want my javascript to replace all urls this way:

"This is my site <A HREF="http://www.try.org/see.html">http://www.try.org/see.html</A> and I would like to raplce it"

Is it possible to make such a replacement with regular expression? (Yes, I can solve such a problem just by splitting string by words and test each word if it is not link) But I really want to know if it is doable by just .replace() function.

Thanks ...
Feb 25 '08 #1
4 1263
acoder
16,027 Expert Mod 8TB
I assume you know how to match a URL. Use parentheses around the reg. exp. for capturing. Then use $1 to refer to the captured match, e.g. the replacement could be <a href="$1">$1</a>.
Feb 25 '08 #2
gits
5,390 Expert Mod 4TB
i just came out the following way:

Expand|Select|Wrap|Line Numbers
  1. var s  = 'This is my site http://www.try.org/see.html and I would like to replace it';
  2. var ma = s.match(/(http[:/\w.]+)/)[1];
  3. var li = '<a href="' + ma + '">' + ma + '</a>';
  4.  
  5. var new_text = s.replace(s1[1], li);
  6.  
  7. alert(new_text);
kind regards
Feb 25 '08 #3
Dasty
101 Expert 100+
Thanks. I really love you guys.

I ended up with this:

Expand|Select|Wrap|Line Numbers
  1. mystr.replace(/((https?|ftp):\/\/[^\s]+)/g, '<a href="$1">$1</a>');
Which is enough for my needs.
Feb 25 '08 #4
acoder
16,027 Expert Mod 8TB
No problem. Glad it helped.
Feb 25 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: php newbie | last post by:
I am getting error messages when I try to delete from a table using the values in the table itself. The intent is to delete all rows from TableA where col_2 matches any of the col_1 values. ...
5
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
2
by: Jeffrey Ganping Chen | last post by:
I'm trying to build a generic runtime C# expression evaluation engine to allow the user to evaluate any C# code blocks at runtime within the application's context. For example, obj4.p4 =...
2
by: Nick Gilbert | last post by:
Hi I have a number of pages where it is valid for the user to enter HTML. On these pages, I have turned off RequestValidation ("ValidateRequest = false" in the page directive) so that the...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
1
by: Matthias S. | last post by:
Hi there, I'm trying to build a regular expression which will do a replace for me. I'm getting grey hair on this one: here is my input string: <a href="http://test.com/fun.jpg"...
12
by: Angus | last post by:
Hello I have a class I am using which raises an exception in its constructor if certain things aren't in place. I can easily create the situation where an exception is raised. If I use the...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.