2179 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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?

1676 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 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
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
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
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
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.

2179 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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! :)

2179 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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
2179 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator says

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

1234 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
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)
2179 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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.

2179 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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