Regular Expressions

S

Scott Schluer

Hi all,

I got a JavaScript function from a website that uses regular expressions to
count the number of words in a textbox. I'm trying to replicate it with
ASP.NET so I can run a second check on the server side to make sure the word
count doesn't go over a specified number of words. I don't know anything
about regular expressions so I'm hoping there's a glaring error that someone
can just point out:

The JavaScript function works great, but the ASP.NET function I tried to
replicate isn't working (it's not doing the .Replace function correctly).
Here's the Javascript function:

function CountWords (this_field, that_field) {
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;

if (fullStr.length < 2) word_count = 0;
that_field.value = word_count;
}

Here's the ASP.NET function:

Public Shared Function CountWords(ByVal strString As String) As Integer
strString = strString & " "
Dim initial_whitespace_rExp As New
System.Text.RegularExpressions.Regex("/^[^A-Za-z0-9]+/gi")
Dim left_trimmedStr As String =
initial_whitespace_rExp.Replace(strString, "")
Dim non_alphanumerics_rExp As New
System.Text.RegularExpressions.Regex("/[^A-Za-z0-9]+/gi")
Dim cleanedStr As String =
non_alphanumerics_rExp.Replace(left_trimmedStr, "")
Dim splitString() As String = cleanedStr.Split(" ")
Dim WordCount = splitString.Length - 1

Return WordCount
End Function

If I type the sentence: "This is a test sentence", the JS function
returns a total of 5 words. The ASP.NET function returns 5 + the number of
EXTRA spaces (I don't know how many extra spaces I typed here, but lets say
it was 10 -- the ASP.NET function would return 15 words). The two Replace
functions should be stripping those.

Any ideas?
 
J

Jeff B

Try this

Public Shared Function CountWords(ByVal strString As String) As Integer
strString = strString & " "
Dim initial_whitespace_rExp As New
System.Text.RegularExpressions.Regex("\A[^\w]*")
Dim left_trimmedStr As String =
initial_whitespace_rExp.Replace(strString, "")
Dim non_alphanumerics_rExp As New
System.Text.RegularExpressions.Regex("[^'\w]\b*")
' REPLACE ALL NON-ALPHNUMERIC EXCEPT APOSTROPHE WITH WHITESPACE
Dim cleanedStr As String =
non_alphanumerics_rExp.Replace(left_trimmedStr, " ")
'REPLACE ALL CONSECUTIVE WHITESPACE WITH SINGLE WHITESPACE
Dim consecutive_whitespace_rExp As New
System.Text.RegularExpressions.Regex("[\s]*[^'\w]")

Dim fixedCleanedStr As String =
consecutive_whitespace_rExp.Replace(cleanedStr, " ")
Dim splitString() As String = fixedCleanedStr.Split(" ")
Dim WordCount = splitString.Length - 1

Return WordCount
End Function



Scott Schluer said:
Hi all,

I got a JavaScript function from a website that uses regular expressions to
count the number of words in a textbox. I'm trying to replicate it with
ASP.NET so I can run a second check on the server side to make sure the word
count doesn't go over a specified number of words. I don't know anything
about regular expressions so I'm hoping there's a glaring error that someone
can just point out:

The JavaScript function works great, but the ASP.NET function I tried to
replicate isn't working (it's not doing the .Replace function correctly).
Here's the Javascript function:

function CountWords (this_field, that_field) {
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;

if (fullStr.length < 2) word_count = 0;
that_field.value = word_count;
}

Here's the ASP.NET function:

Public Shared Function CountWords(ByVal strString As String) As Integer
strString = strString & " "
Dim initial_whitespace_rExp As New
System.Text.RegularExpressions.Regex("/^[^A-Za-z0-9]+/gi")
Dim left_trimmedStr As String =
initial_whitespace_rExp.Replace(strString, "")
Dim non_alphanumerics_rExp As New
System.Text.RegularExpressions.Regex("/[^A-Za-z0-9]+/gi")
Dim cleanedStr As String =
non_alphanumerics_rExp.Replace(left_trimmedStr, "")
Dim splitString() As String = cleanedStr.Split(" ")
Dim WordCount = splitString.Length - 1

Return WordCount
End Function

If I type the sentence: "This is a test sentence", the JS function
returns a total of 5 words. The ASP.NET function returns 5 + the number of
EXTRA spaces (I don't know how many extra spaces I typed here, but lets say
it was 10 -- the ASP.NET function would return 15 words). The two Replace
functions should be stripping those.

Any ideas?
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top