在ASP中使用替换字符串

时间:2013-06-03 11:11:41

标签: javascript regex asp-classic replace

我在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;
}

问题是当我改用ASP时,似乎无法正常工作:

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

它没有从函数返回任何内容,如何解决问题?感谢

已更改为

'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

仍然没有运气请帮助..

1 个答案:

答案 0 :(得分:2)

您需要使用正则表达式对象,如下所示:

'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, " ")

同时从模式周围删除/

<强>已更新

'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