user1871516
user1871516

Reputation: 1009

Using replace string in ASP

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

Upvotes: 0

Views: 2858

Answers (1)

DAC84
DAC84

Reputation: 1274

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

Upvotes: 2

Related Questions