Hi!
Sorry for my bad english!
I want to make a function that will replace "[LINK=url]Some text[/LINK]" with "<a href=url>Some text</a>".
I tried, but the code is too long. Is there any shorter code for this.
Here's how I do this (but this code make only first [LINK] into <a href...>)

<script type="text/javascript">
function links(){
text = document.getElementById('tekst').value;
linkOpen = text.indexOf("[LINK=");
linkClose = text.indexOf("[/LINK]");
len = text.length;
forLink = text.substring(linkOpen,linkClose);
linkA = forLink.split("]");
linkB = linkA[0];
linkText = linkA[1];
linkB = split("=")
link = linkB[1];
linkClose += 7;
part_1 = text.substring(0,linkOpen);
part_2 = text.substring(linkClose,len);
all = part_1 + "<a href=" + link + ">" + linkText + "</a>" + part_2;
}
</script>
<script type="text/javascript">
var str="[LINK=url]This is some text[/LINK]\n[LINK=url]Anather text[/LINK]";
str = str.replace(/\[LINK/g, "<a href");
str = str.replace(/\[\/LINK\]/g, "</a>");
str = str.replace(/\]/g, ">");
alert(str)
</script>

Try this it will doyour work.
But it can be done in a better way also.

What if the user writes ]?
eg. user writes this (text between ""):
"x=2+[3-2(5+9)+3]
[LINK=http://www.somesite.com/]Here[/LINK] you can find the other examples."
The output will be:
"
x=2+[3-2(5+9)+3>
...
"
Can you change it?
Thanks!

try:

function links(str){
return str.replace(/\[link\s*=\s*(['"])?(.*?)\1\s*\](.+?)\[\/link\]/ig,'<a href="$2">$3</a>').replace(/\[link\](.+?)\[\/link\]/ig,'<a href="$1">$1</a>');
}

Can you explain this?
(Or if there is on the internet some page that has tutorial.)
Thanks!

Look for tutorials on Regular Expressions.

Thank you very much!

In case it is not clear, on the function I posted earlier, you need to pass the text where you have the [link] tags to html <a> tags:

var text = document.getElementById('tekst').value;
var result = links(text);
alert(result)

Ok. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.