RegExp thats too complicated for me

I

infinull

I am using a javascript rich-text-editor to allow a content writer to
easily see what there content looks like, however, the content
management system that I have made would like to accept bbcode style
text. The problem is the text editor ueses spans with the style
attribute to get the desired effect(it uses the mozilla rich-text
editing api), I am looking at changing very complex spans:
<span style="text-decoration: underline, font-weight: bolder,
text-style: italic">
some text</span>
into very simple bbcode:
some text

I think I can manage links, horizontal rules and h# tags, but this
seems a little over my head, I cannot think of a way that I know will
work, and I think the answer is a regular expression(this executed on
the string before it is sent to the server), but I am not sure, and
would like some input, as opposed to struggling around with some crazy
thing that I probably won't be able to get to work.
 
M

Michael Winter

[snip]
<span style="text-decoration: underline, font-weight: bolder,
text-style: italic">
some text</span>
into very simple bbcode:
some text

[snip]

This might only be a partial solution. If it is, you'll need to provide
some more information. For example, will you be sending strings with many
such tags, or just one? If it's many, what could appear adjacent to the
SPAN elements?

/<span\s+style\s*=\s*"(.+)"\s*>(.+)<\/span>/

The regular expression above will match the SPAN you have shown above,
remembering the content inside the style attribute, and the content of the
SPAN.

The following regular expressions will match the text-decoration,
font-weight, and text-style properties, respectively. Note that they don't
check what value those properties are assigned.

/text\-decoration:\s+[a-z]+/
/font\-weight:\s+[a-z]+/
/text\-style:\s+[a-z]+/

A rather artificial example:

var html = '<span style="font-weight: bolder;' +
' text-decoration: underline; text-style: italic">some text<\/span>';
var s = /<span\s+style\s*=\s*"(.+)"\s*>(.+)<\/span>/.exec(html),
c = s[2],
u = /text\-decoration:\s+[a-z]+/.test(s[1]),
b = /font\-weight:\s+[a-z]+/.test(s[1]),
i = /text\-style:\s+[a-z]+/.test(s[1]);

if(u) {c = '' + c + '';}
if(i) {c = '' + c + '';}
if(b) {c = '' + c + '';}

alert(c);

You'll have to give that thorough testing, as I cannot: I don't know what
your data will look like.

Hope that helps,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top