find position af _alle_ matches ved regexp (vbScript til JS)
Hejsa - Jeg har følgende kode i VBScript...Er det mulig at få samme funktionalitet i JavaScript...
det, det handler om, er at jeg skal finde positionerne af matches af et pattern i en string....
bruger jeg match() får jeg en array af de forskellige matches - bruger jeg search() får jeg indexet af det første match...
hvad jeg gerne vil have er positionerne af _alle_ matches i strengen...
Jeg er så faldet over noget VBScript kode, som gør det -- men er der en, som kan oversætte det til Javascript for mig ?
[:code start]
<%
'this sub finds the matches
Sub RegExpTest(strMatchPattern, strPhrase)
'create variables
Dim objRegEx, Match, Matches, StrReturnStr
'create instance of RegExp object
Set objRegEx = New RegExp
'find all matches
objRegEx.Global = True
'set case insensitive
objRegEx.IgnoreCase = True
'set the pattern
objRegEx.Pattern = strMatchPattern
'create the collection of matches
Set Matches = objRegEx.Execute(strPhrase)
'print out all matches
For Each Match in Matches
strReturnStr = "Match found at position "
strReturnStr = strReturnStr & Match.FirstIndex & ". Match Value is '"
strReturnStr = strReturnStr & Match.value & "'." & "<BR>" & VBCrLf
Response.Write(strReturnStr)
Next
End Sub
'call the subroutine
RegExpTest "is.", "Is1 is2 Is3 is4"
%>
Output:
Match found at position 0. Match Value is 'Is1'.
Match found at position 4. Match Value is 'is2'.
Match found at position 8. Match Value is 'Is3'.
Match found at position 12. Match Value is 'is4'.
[:code end]
/Søren
