469,360 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

regexp test for italics and bold

Bob
Hello:

I could use some regular expression help. I'm trying to create a
client-side javascript function that will test user input to determine
the markup of text that has been copied into a textarea input field,
then will insert html tags accordingly.

So, the user writes up a document in word that contains instances of
italics, bold, and underline. The user then copies the text of that
document into my textarea field. I am trying to create a function that
will then search through all instances of that markup and insert the
appropriate html tags directly into the textarea field.

My problem is that I cannot find a way in javascript with regexp or
otherwise to test user input for italics, bold, or underline. I found
something similar in a PHP script here:

// Check for Italics text
$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span
class="italics">$1</span>',$Text);

But I cannot find a client-side javascript equivalent to this. This
only needs to work in the latest IE, as this is part of an internal
tool to help build HTML emails.

Any help would be appreciated greatly.

Thanks,

Bob

Oct 25 '05 #1
13 3583
In article <11*********************@g47g2000cwa.googlegroups. com>,
"Bob" <bo********@gmail.com> wrote:
Hello:

I could use some regular expression help. I'm trying to create a
client-side javascript function that will test user input to determine
the markup of text that has been copied into a textarea input field,
then will insert html tags accordingly.

So, the user writes up a document in word that contains instances of
italics, bold, and underline. The user then copies the text of that
document into my textarea field. I am trying to create a function that
will then search through all instances of that markup and insert the
appropriate html tags directly into the textarea field.

My problem is that I cannot find a way in javascript with regexp or
otherwise to test user input for italics, bold, or underline. I found
something similar in a PHP script here:

// Check for Italics text
$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span
class="italics">$1</span>',$Text);

But I cannot find a client-side javascript equivalent to this. This
only needs to work in the latest IE, as this is part of an internal
tool to help build HTML emails.

Any help would be appreciated greatly.

Thanks,

Bob


Use the DOM:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
<meta name="generator" content="BBEdit 8.2" />
<script>
function seekItal() {
itals = document.getElementsByTagName("i");
for (i = 0; i < itals.length; i++) {
alert(itals.item(i).firstChild.data); }
}
</script>
</head>
<body onLoad="seekItal();">
<i>a test</i> more text <i>italic</i>
</body>
</html>
Oct 25 '05 #2
Bob
That's not quite what I'm going for. That will search through the html
document itself for text that is surrounded by italics tags. But what
I'm trying to do is search through user input: whatever the user copies
into the textarea field. That content may have attributes like bolding
and italics that are carried over from the original source (e.g., MS
Word) and I am hoping to detect these attributes through regular
expressions and add tags into the textarea field accordingly.

Oct 25 '05 #3
Sorry textarea elements dont support rich formatting, you need a rich
text iframe text editor, like PowerWEB TextBox, or HtmlArea, or
FreeTextBox etc etc etc...

Once you're using one of those you can do this, you access the
..innerHTML of the iframe's body element, and go from there.

Good luck.

Jim

Oct 25 '05 #4
Bob
Thanks for the reply. I checked out some of the solutions you listed.
I could be out of my depth, but they seem to take a textarea field and
apply some javascript magickery to get their effect, which leads me to
believe that my goal should be possible. The input starts out in the
textarea field, so there has to be some way to determine attributes,
iframes or no. I'd rather not use any of these packaged solutions
because their output code is pretty ugly.

Oct 25 '05 #5
Trust me, they're not applying it to a text box, they are infact
mirroring the textarea with an iframe/div they (sometimes) create
dynamically.

It's based on this
http://msdn.microsoft.com/library/de...leditplatf.asp

If you implement that yourself you can strip all the formatting
stuff...

Jim

Oct 25 '05 #6
Bob
Just to clarify, I don't care what it looks like in the textarea field.
I just want to detect if it was italicized when it was copied over and
then add the tags <i>foo</i> appropriately. It can all look like
straight text in the textarea field.

Oct 25 '05 #7
Sure. Textareas don't output html tags - remember they were created
about a decade ago when everyone was just pleased to get online and
share views on who was the best star trek captain... ;)

Oct 25 '05 #8
Bob
I don't expect the textarea to output tags. I will insert the tags
directly into the textarea with the script. So, for example, a user
copies in "This is italics. This is not." from a Word document and the
first sentence is in italics. Then my script gets run and recognizes
that the first sentence is italicized and inserts the tags directly
into the textarea.value string to change it to "<i>This is italics.</i>
This is not." I'd like to do something similar to what this PHP code
is supposed to do:

// Check for Italics text
$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span
class="italics">$1</span>',$Text);

Oct 25 '05 #9
Then what I'd suggest you do is;

a) add an iframe with contentEditable set true, to your page

b) have users paste word documents into this

c) listen to the paste event in the iframe.document object

d) get the content of the iframe with iframe.document.body.innerHTML,
this will be your Word document in HTML markup (in my view this is
pretty amazing - such as easy way to convert a word doc to html markup,
AND on the client side!)

e) do what Mark Twain suggested to the content you got from d)

f) insert it into a textarea with textareaID.value = ....

Jim

Oct 25 '05 #10
I think what I've failed to impart to you is that once your users
pastes into a textarea, all the formatting is gone - no way AT ALL to
access any kind of formatting info.

My last suggestion above is in my opinion (8 yrs of comm. web
development) the only option outside of using some sort of ActiveX or
plugin on the client side...
Jim

Oct 25 '05 #11
Lee
Bob said:

Just to clarify, I don't care what it looks like in the textarea field.
I just want to detect if it was italicized when it was copied over


Only characters are pasted.
Information about formating never makes it into the textarea.
There is nothing to detect.

Oct 25 '05 #12
Bob
Thank you for the replies and patient explanation. I'll check further
into your suggested iframes solution.

Oct 26 '05 #13
Bob wrote:
Hello:

I could use some regular expression help. I'm trying to create a
client-side javascript function that will test user input to determine
the markup of text that has been copied into a textarea input field,
then will insert html tags accordingly.
Some of my thoughts for you.
So, the user writes up a document in word that contains instances of
italics, bold, and underline.
The user then copies the text of that
document into my textarea field. I am trying to create a function that
will then search through all instances of that markup and insert the
appropriate html tags directly into the textarea field.
When you refer to "instances that mark-up" what mark-up are you
referring to? I assume the underlying .doc format. In which case are
you looking for someone who is familiar with .doc formats as well, in
order to be able to write a RegExp?

Also, as others have noted, when you cut and paste .doc text into a
TEXTAREA, so all .doc formatting is stripped out. How to you then
plan to get hold of the .doc formatting?
My problem is that I cannot find a way in javascript with regexp or
otherwise to test user input for italics, bold, or underline. I found
something similar in a PHP script here:

// Check for Italics text
$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span
class="italics">$1</span>',$Text);

But I cannot find a client-side javascript equivalent to this. This
only needs to work in the latest IE, as this is part of an internal
tool to help build HTML emails.


Some options are:-

(a) Use a content mark-up, as used for Wiki or some other news groups
(e.g. http://www.webdeveloper.com). I.e. if the user wants italics,
they must physically mark it up themselves. But this is
obviously not idea.

(b) As suggested by others, use the rich text edit components in the
latest browsers (e.g. contentEditable="true" DIVS or designMode="on"
IFRAMES). When you cut and paste, formatting is preserved, although
the automatically generated HTML is not the best. Equally, users can
type directly into those rich text components and create mark-up
directly.

(c) Create some form of Macro in Word to talk to the web page, or try
to get the web page to talk to (i.e. automate) Microsoft Word, using
the ActiveXObject ("Word.Application"), and directly inspect the
Document Object Model for Microsoft Word to extract the formatting.
These are likely to be difficult to implement.

Julian

Oct 26 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Anand Pillai | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
11 posts views Thread by HopfZ | last post: by
3 posts views Thread by prt7u | last post: by
2 posts views Thread by Nathan Sokalski | last post: by
4 posts views Thread by Matt | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.