0

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

仍然没有运气请帮助..

4

1 回答 1

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
于 2013-06-03T11:19:39.393 回答