0

I have a working function in JS

function countWords(s){
    s = s.replace(/(^\s*)|(\s*$)/gi,""); //modified trim function
    s = s.replace(/[ ]{2,}/gi," ");
    s = s.replace(/\n /,"\n");
    return s.split(' ').length;
}

The problem is when i change to ASP, it seems not working:

   Sub formatText(a)
        a = Replace("/(^\s*)|(\s*$)/gi",a,"")
        a = Replace("/[ ]{2,}/gi",a,"")
        a = Replace("/\n /",a,"\n")
        return a
    End Sub

It return nothing from the function, how to fix the problem? thanks

Changed to

'regEx initialization
Dim regEx
set regEx = New RegExp  'Creates a regexp object
regEx.IgnoreCase = True 'Set case sensitivity
regEx.Global = True     'Global applicability

'trim input text
Sub formatText(a)
    a = Replace("(^\s*)|(\s*$)",a,"")
    a = Replace("[ ]{2,}",a,"")
    regEx.IgnoreCase = False 'Set case sensitivity
    regEx.Global = False     'Global applicability
    a = Replace("\n ",a,"\n")
    return a
End Sub

still no luck please help..

2
  • 1
    The / at the beginning and end of your regexes is JavaScript regex literal syntax, not part of the actual regex expressions. You don't want it in your ASP version, though I don't know how to set the g and i flags or their equivalent in VB. Commented Jun 3, 2013 at 11:13
  • 1
    May be your problem will resolve from this link stackoverflow.com/questions/8219317/… Commented Jun 3, 2013 at 11:14

1 Answer 1

2

You'll need to use a regex object, like so:

'regEx initialization
Dim regEx
set regEx = New RegExp  'Creates a regexp object
regEx.IgnoreCase = True 'Set case sensitivity
regEx.Global = True     'Global applicability

regEx.Pattern = "<[^>]*>" 'Remove all HTML
strTextToStrip = regEx.Replace(strTextToStrip, " ")

Also remove the / from around the pattern.

UPDATED

'trim input text
Function formatText(a)
    Dim regEx
    set regEx = New RegExp  'Creates a regexp object
    regEx.IgnoreCase = True 'Set case sensitivity
    regEx.Global = True     'Global applicability

    regEx.Pattern = "(^\s*)|(\s*$)"
    a = regEx.Replace(a, "")

    regEx.Pattern = "[ ]{2,}"
    a = regEx.Replace(a, "")

    formatText = a
End Function
Sign up to request clarification or add additional context in comments.

3 Comments

is using count as -1 can do the same thing? Replace(expression, find, replacewith[, start[, count[, compare]]])
sorry but would you mind have a look at my changed version, why it still return nothing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.