.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Regular Expression Word Count :: Problem when word wrapped in "", need "eg" to be counted as 1 word

Posted By:      Posted Date: December 04, 2010    Points: 0   Category :.NET Framework

Hi very new to regular expression, i have the following js function to return the number of words displayed in a text area.


 var matches = textarea.replace(/<[^<|>]+?>|&nbsp;/gi,' ').match(/\b/g);
 var count = 0;
 if(matches) {
  count = matches.length/2;


This function works perfectly except when a word is contained in double quotes: eg "word"

This will come back as 3 words, and I need it to be 1

Any help would be much appreciated.


View Complete Post

More Related Resource Links

Regular expression for Ms Word HTML Markup


I am trying to create a regular expression that searchs for all html tags and attributes that start with 'mso', ending with a semi-colon.  If found, it would replace it with an empty string.

eg. Input string:

<span style="font-family: Arial, sans-serif; font-size: 11pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Times New Roman; mso-bidi-font-family: Times New Roman; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">My text goes here</span>


<span style="font-family: Arial, sans-serif; font-size: 11pt;">My text goes here</span>

All the attempts I have tried have yielded no results. I have tried: (\bmso)\w*[;]\b and this - \bmso\w+(?=;\b) among many other variations of the two.  Any help would be appreciated.


Regular expression for MS Word HTML Markup


I am trying to validate a rich text box to remove some ms word content but I want to keep the other attributes that are not MS office related.

input: <span style="font-size: 11pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: &#34;Times New Roman&#34;; mso-bidi-font-family: Times New Roman&#34;; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; font-family: Arial&#34;, &#34;sans-serif&#34;">mso-bidi-language: AR-SA; mso-My Text Here; mso-My Text Here2</span>

I first run this regex to remove the unformed html (\&\#34\;).  Then, I remove everything inside the =" ending with "

regex: (?<=\=\")(.*?)(?=\")
: <span font="" style="">mso-bidi-language: AR-SA; mso-My Text Here; mso-My Text Here2</span>

Now it removes everything inside the tags and not the text. With the following, it removes the matching pattern, but it also removes the text that i need and the end tags.

: <span font="font-size: 11pt;" style="font-family: Arial, sans-serif;">

When i try and combine the two expressions, it does not provide the correct output.

Charset Problem with dynamically generated Word Document


in my asp.net web application i am dynamically generating a word documnet out of a sql data. I have set the char set UTF-8 during the doument creation in code but still when i open the generated document and check it char set .. it is always windows-1252 not the one that i have set. My sql data in the document have special characters whihc can only be understandable using the UTF-8 charset otherwise its always ?T written in place of an apostrophy . Pasting my code here..

         HttpContext.Current.Response.ContentType = "application/msword";
         HttpContext.Current.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
         HttpContext.Current.Response.Charset = "utf-8";
         string strFileName = "Details" + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        StringBuilder strHTMLContent = new StringBuilder();
        string strMessage = " ";
        DataView abstractview = (DataView)dsAbstract.Select(DataSourceSelectArguments.Empty);
        foreach (DataRowView row1 in abstractview)
            strMessage += "<table border='1' cellspacing='0' cellpadding='5' width

Generating MS Word doc from mht file gives smart quote problem


I have a word document to be generated using ASP.Net/VB.net.

So I have a word document template saved as .mht file on the web server.

In my ASP.Net code,I read this mht file content and replace certain string line Description$$ with the actual description from the database.Then save this content as .doc file.

This works fine but shows a box symbol when I have smart quotes " (not this one as I cannot type it here.The double quote which you see in MS Word,the slanted double quote).

I dont know how to make my word doc generated, show the exact double quote as in MS word.

If any of you have any idea of this please reply.

Thank you

Html editor word wrap problem

I created an html editor which uses an iframe for designing. The iframe doesnt wraps the text within it.... but the text keeps on extending in the same row towards the right. i tried setting scrolling='no' but it didnt work... tried word-wrap:break-word but thats also of no use........ please help! 

Microsoft Word Mail Merge Problem


I wrote code to create label from Microsoft Word Mail Merge using Visual Studio 2008 ASP.NET C#. This is my code:

public void CreateLabel(string strCSVPath, string strTemplatePath)
            Object fileName = strTemplatePath;

            Object missing = System.Reflection.Missing.Value;
            Object oEmpty = "";
            Object oFalse = false;
            Object oTrue = true;
            Object oAutoFormat = word.WdOpenFormat.wdOpenFormatAuto;

            word.Application wApp = new word.Application();
            word.Document wDoc = wApp.Documents.Open(ref fileName,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing);
            word.MailMerge wMM = wDoc.MailMerge;

            wMM.OpenDataSource(strCSVPath, ref oAutoFormat, ref oTrue, ref oFalse, ref oTrue,
                                ref oFalse, ref oEmpty, ref oEmpty, ref oFalse, ref oEmpty, ref oEmpty,
                                ref oEmpty, ref oEmpty, ref oEmpty, ref missing, ref missing);

            wMM.DataSource.QueryString = "SELECT * FROM " + strCSVPath;

            wMM.Destination = word.WdMailMergeDestination.wdSendToNewDocument;


Reporting Services 2008 - footer problem when exporting to Word


I have a footer that I want to show up on the last page only. 

I use the following expression for the text box under text box properties --> visability

=IIF(Globals!TotalPages = Globals!PageNumber,

False, True)

When I run the report in reporting services, it does only print the footer on the last page.  When exported to Word, footer prints on all pages.

My questions: 

1.  Is my expression ignored in Word for the footer, so I will not be able to do this in the footer?

2.  Is there another way to have something print at the bottom of the last page that will be recognized when exported to MS Word (report size will vary based on data returned)? 

Any help is appreciated.

Kind regards.

Content Type and Word properties problem



I defined a Site collection content type that have some required properties  and added it to a document library .

When we create a word document and then we try to save it in that document library , we have this message saying that some properties values are missing if we don't fill the mandatory ones

by pressing the button "Go to Document Information Panel"  the  “Retry  save” 

Word Web App cannot open this document due to an unknown error. If the problem persists, try opening

Word Web App cannot open this document due to an unknown error. If the problem persists, try opening the file in Microsoft Word.

Can anybody pls help me how to resolve this for which got strucked !!!!


Cheers !!


Thanks in Advance !!! Cheers !!! Praveen

Word Web App cannot open this document due to an unknown error. If the problem persists, try opening


Word Web App cannot open this document due to an unknown error. If the problem persists, try opening the file in Microsoft Word.

Hii ,

Can anybody pls help me how to resolve the above Error am getting in Sharepoint Foundation with the use Office WebApps installed on the same development server,when Opening a word document after it got created.






Thanks in Advance !!! Cheers !!! Praveen Mandalapu

Reportviewer 2010 redistributable export to Ms Word pagenumber problem


Hi, in my asp.net app I use the Reportviewer 2010 tot show reports (RDLC). This is working fine, but when I export to Word the pagenumbers is always 1 from 1 even when the document contains 2 or more page, while when I export to PDF it is showing the right page number.

Anyone has the same problem?

Word Automation through Windows service (Windows Server 2008) - Problem

I went through the thread in this link http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/65a355ce-49c1-47f1-8c12-d9cf5f23c53e (which is a very similar problem to mine), but couldnt solve my problem.
I seem to face a very related problem in word automation.
I have created a windows service which creates a sample word document (WORD 2007) and then converts it to a .doc file (WORD 2003 format) or a PDF file , based on requirement.  Let me post the sample code here :

Word Count of a textbox in a DetailsView



Has anyone any advice or resources that they could provide with regards having a word count of a mulitline textbox that is found within a DetailsView?  I have tried using JavaScipt and a this page: http://aspnet.4guysfromrolla.com/demos/printPage.aspx?path=/articles/103107-1.aspx - but I don't seem to be able to get it working within that DetailsView,



PrintPocketCE Print Pocket Excel, Pocket Word and email files

Version 3.560 (May 15, 2009):

Important bug fix: a slight difference in how some devices create fonts was causing a few devices to have significantly longer print times for large print jobs.

Adjusted PocketJet printer paper feed commands

Other minor fixes and enhancements.

Version 3.559 (Mar 5, 2009):

Added support for Martel MCP78xx printers

Version 3.558 (Aug 20, 2008):

Added support for Martel MCP78xx printers

Version 3.557 (June 27, 2008):

Fixed error in Canon printer support

Fixed COM0 port selection

Version 3.556 (June 15, 2008):

Added support for Brother MW-260

Added support for Sato MB400

Version 3.555 (Aug 8, 2007):

Added support for Pentax RuggedJet 3 and RuggedJet 4 printers

Changed Epson TM-P60 support to maximum page width of 1200 dots

Version 3.551 (Aug 1, 2006):

Added Peripheral Nomad printer support

Version 3.550 (Mar 23, 2006):

Added Panasonic JT-H200PR printer support

Added Pocket Spectrum printer support

Fixed problem with WM5.0 "inverted image" problem

MS Word style comments in asp.net



I need to create a system, that will be similar to ms word style comments. So the idea is the following: on the page there are a pack of text boxes labels and so on, and user need have the way to comment each control, and save commented text, for example to DB, so the others can reed comments too.

I tried PopUpControlExtender, but it works in single way, and i cannot view all the popups in one time. Or may be i missed something and there is a way to popup all the controls at one time? 

Therefore, are there any known solutions for a discribed task, based on PopUpControlExtender or something else?



Is it possible with SQL sorted in descending order according to the word?
In Table I shrajeno Mastercard, Visa, Diners etc ...

All Visa are the first in the order, another Mastercard, etc. ...

Something does not work: ORDER BY PaymentType = 'Visa';

I would not want to work in one field PaymentId (111,222,33, etc. ..) and then use the ORDER BY.

Is this possible in such a way?

how get only two word in database?



i have a feild in database where we store data like







i want only first to word to show in textbox1 like(only PT).

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend