2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Hi guys, I have a problem I am loading some text with HTML formating, the problem is that the formating is not normal HTML instead of using

<i></i> or <b></b>
the text uses symbols like this is bold or ’’this text will be italic’’. The question is how can I change those into HTML tags? I need to know when the tag ends and when begins but the closing and opening symbols in text look exactly the same. Any ideas?

1677 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

I need to know when the tag ends and when begins but the closing and opening symbols in text look exactly the same. Any ideas?

You could convert them manually from the beginning to end if the tags are not overlapping :D

Where did you find this text anyway?

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
MBMedia says

You say the start and end symbols look the same, but what exactly do they look like? Can you give us a verbatim excerpt from the text?

Because you can likely still manage it with a regular expression or at worst a simple loop even though the start and end tokens the same. The only difference is that it would just fail harder if there was a syntax error in the text.

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
MBMedia says

For example:

var str: String = 'Lorem ipsum sit amet dolor "this is italic" lorem ipsum';
var newStr: String = str.replace(/"(.*?)"/gs, '<i>$1</i>');
trace(newStr);

Does exactly what you need for if italics are always surrounded by a ”. The only difference between a properly thought out syntax (where beginning and ending tokens are different) is that if there’s a syntax error (using a quote where it shouldn’t be) it will throw off the formatting from that point in the text onward, whereas a properly designed syntax would only fail for the specific place where the error was.

EDIT : make sure to use the .*? instead of just .* like I originally posted, .* is a greedy match .*? is a lazy match and you need a lazy match for it to work right. And use /gs flags if you want it to work across multiple lines, only /g flag if you don’t.

2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Hi, guys this text is driven from the Wikipedia API here you can see the formatting syntax http://www.mediawiki.org/wiki/Help:Formatting MBMedia I will check your solution seems it should work! :)

2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says
It works for me, but I have a problem with one example lets say I have code like this:
var str:String = "[[something]]";
str = str.replace(/[[(.*?)]]/gs, '<a href="#">$1</a>');
trace(str)
//what I get in trace is "[[something]]", why the brackets are not changed? :O
2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Any idea why method replace doesn’t recognize square brackets( [ )? ? ;/

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
MBMedia says

Because square brackets are part of RegExp syntax, so they need to be escaped if you want them interpreted as just characters in your pattern.

var str:String = "[[something]]";
str = str.replace(/\[\[(.*?)\]\]/gs, '<a href="#">$1</a>');
trace(str)
2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Because square brackets are part of RegExp syntax, so they need to be escaped if you want them interpreted as just characters in your pattern.
var str:String = "[[something]]";
str = str.replace(/\[\[(.*?)\]\]/gs, '<a href="#">$1</a>');
trace(str)

Great that works! :)

Few more question, I want to change something from File: to | I am having trouble for example “File:this is a file.jpg|Something” to “Something”

Also I am having trouble to change “heart”. into something like “heart”.

Additionally is there a way to remove white space + text from point to point or point to end of file?

P.S Never worked with such string replacement before sorry for all the questions.

2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Also how to say that I want to replace from | to first space? This doesnt seem to work becuase it doesn’t stop on the first space it takes all the spaces there are ;P

str = str.replace(/\|(.*?) /gs, '');

by
by
by
by
by
by