473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3912
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I want to write a _very_ simple text parser that would replace a string like: "This is text with /italics/, *bold* and _underline_." and generate automatically something like this: ...
10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
9
by: Laphan | last post by:
Hi All I'm using the below to limit the input into a text box to just letters, numbers, hyphens and full stops, but I also need to allow the backspace, delete and arrow keys to come through. ...
3
by: prt7u | last post by:
Howdy, I'm new to CSS and have a basic question. What is the best way to italicize a word or words using CSS versus the <em</emtags that used to be used in HTML? Or am I jumping the gun and...
2
by: Nathan Sokalski | last post by:
I have the following script that I am using to test some JavaScript RegExp code: function RE() { var testing1=new RegExp("*"); var testing2=new RegExp("{0,}"); var testing3=new RegExp("+");...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.