How can I use regular expressions in a calculation field

Hi.

I have an question about regular expressions. I want to use it to change text starting with http://, www and ending with .extention to a clickable link using regular expressions. Usings a calculation field.

This is the code I made.

if(info_content) {
 var vContent = info_content
 
 vContent.replace('http\:\/\/*','<a href="1">1</a>')
 
 return vContent;
} else {
 return null
}

But this doesnt work. Can anyone help me out please?

Debugging calculation is not easy, test the regexp in a method and when you are sure that it works correctly move it to the calculation.

Hi Rick,

There are a few issues in your code. First of all a regexp definition is not a string. You put the regexp definition between forward slashes.
Also to apply the matched text in the resultstring you need to prefix them the dollar sign ($1 to $9) and put your regexp definition that corresponds with that between parenthesis.
Last issue is that a replace function returns the result.
So when you fix this then your code looks something like this:

if(info_content) { 
  
 return info_content.replace(/(http\:\/\/.+)/,'<a href="$1">$1</a>');
  
  
} else { 
 return null;
}

Hope this helps.

The forward slashes are not really mandatory but preferred as far as I know.

The * or + signs will search for 0 or more or 1 or more occurences.

The problem I see is that you have no end defined so your will search in an eager way without limit.
Maybe it is better to search for a space at the end when your url is part of a text (if not you can disregard).
You then also need to make the regex ‘non eager’ by replacing the .* with .?.
You regex would become:```
/(http://.?
\s)/

oke let me explain a bit more:

For example I have this text in a textfield:

this is a test
http://www.google.nl  hi there here I am
hi there
http://www.yahoo.com 
this is a test

I want to have a regular expression (or something else that works) that ’ fishes’ all the url’s out of the text, so I want to have back:

http://www.google.nl
http://www.yahoo.com

If I have this, I can make a calculation easily that makes this URL clickable in a HTML-AREA. :)

Hope I have explained it a bit more.

As said before this /(http\:\/\/.?*\s)/ should work but now with a g added (g = global) ```
/(http://.?*\s)/g

IT2Be:
As said before this /(http\:\/\/.?*\s)/ should work but now with a g added (g = global) ```
/(http://.?*\s)/g

This indeed ‘fishes’ the urls out of it. But I get this:

http://www.google.com hi there here
http://www.yahoo.com

Is there a way to get rid of that ‘hi there here’?

Rick, I think I made a mistake.

Replace ?* with *?

Yep, this is the one```
/(http://.*?\s)/g

BTW this one is better, it will skip that space…

string.replace(/(http\:\/\/.*?)(\s)/g,'<a href="$1">$1</a>$2')

Ah ok. I see :)
Well thank you.

And humans can make mistakes ;)

IT2Be:
BTW this one is better, it will skip that space…

string.replace(/(http\:\/\/.*?)(\s)/g,'<a href="$1">$1</a>$2')

Ok but I use the string.match(/(http://.*?)(\s)/g) now.
But It only works with an whitespace at the end of the url.
Is it possible to do it without the whitespace also?

Yes, but now you have to start reading documentation!

You can replace (\s) with (\s?) or when you need more characters you could do (\s|.)? and any type of variation.
But again, you need to pick up some documentation now because this could end up in an endless thread (and therefore will not end otherwise it would not be endless)…

Ok thanks alot.
I think it will do it now :)