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?
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?