Conversion from a JavaScript regular expression to a VBScript regular expression.

Source: Internet
Author: User

Author: A Lai (Email: A at lai.com.cn homepage: http://www.9499.net blog: http://blog.csdn.net/laily)

Keywords: Regular Expression VB VBScript Javascript

Abstract: The conversion from a JavaScript regular expression to a VBScript regular expression also describes the usage of a regular expression in VB.

Previously, I used JavaScript to write a regular expression algorithm that splits a path into a root directory and a subdirectory string. Because the project needs to write the same function using VBScript. The following is the original jsscript program:
// Purpose: to setparate the path string into two part: the root and the subfolder
// IE.
// C:/AA/BB/CC => C:/+ AA/BB/CC
/// AA/BB/CC => // AA + BB/CC
/// Http://www.9499.net => http: // + www.9499.net
VaR strroot, strsub
VaR Re =/^ ([^/] + [///] + | ///[^/] +) (. *) $/
If (Re. Test (strfolder ))
{
Strroot = Regexp. $1
Strsub = Regexp. $2
}
Because the Javascript language integrates the regular expression function, it feels smooth and natural to write. In VB6, the Regular Expression Function of VBScript can be called only through component objects. In the IDE of VB6, select project> references and find and select Microsoft VBScript regular expression. Note that there are two versions available, one is 1.0 and the other is 5.5. Of course we chose 5.5. The difference between the two versions is that the sub-match submatchs function is missing from the 1.0 version. If you select 1.0, you cannot implement the functions of the above JavaScript code, because the key is to use submatchs matching.
The following is the program code of VBScript which has the same functions as the preceding JScript:
'Purpose: To setparate the path string into two part: the root and the subfolder
'Ie.
'C:/AA/BB/CC => C: // + AA/BB/CC
'// AA/BB/CC => // AA + BB/CC
'Http://www.9499.net => http: // + www.9499.net
Dim strroot, strsub
Dim regpathparse
Dim rematch, rematchcol, resubmatch
Re. pattern = "^ ([^/] + [///] + | ///[^/] + )(. *) $"
Set rematchcol = Re. Execute (strfolder)
If rematchcol. Count = 1 then
Set rematch = rematchcol (0)
Set resubmatch = rematch. submatches
If resubmatch. Count = 2 then
Strroot = resubmatch (0)
Strsub = resubmatch (1)
End if
End if
How about JScript Regular Expression writing is much simpler than vbs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.