2

I want to use JavaScript regex to list all string values used in VBA code.

Example:

If cmd = "History Report" Then
     Range("D2").Formula = "=TEXT(""" & createDate & """,""" & Replace(subtitleFormat, """", """""") & """)"
     comment = "This task is completed!"
End If

The result will be:

1> "History Report"
2> "D2"
3> "=TEXT("""
4> ""","""
5> """"
6> """"""
7> """)"
8> "This task is completed!"

I'm so glad to have some better ideas to solve this problem.

1
  • 1
    You can't use JavaScript regex in VBA. You can use the Microsoft Scripting Runtime library to use VBScript-flavor regex though. Commented Jun 2, 2015 at 18:23

2 Answers 2

2
""*[^"]*"*"

Try this.See demo.

https://regex101.com/r/pG1kU1/19

Edited:

"(?:"{2})*[^"]*(?:"{2})*"

Try this.See demo.

https://regex101.com/r/pG1kU1/23

Sign up to request clarification or add additional context in comments.

3 Comments

@ZuZu Please mark vks' solution as the answer if it worked for what you needed.
I have fixed a mistake with example. In VBA, string value can contain double quote by double up on the quotes. I want to capture whole string that can contains double quote such as: "I need ""your help"" in this problem".
@ZuZu: I have a regex solution that will correctly deal with the VBA string literals.
1

You need to account for double quotation marks inside string literals:

"((?:""|[^"])*)"

This will match all them. Capturing group 1 will hold the strings themselves. If you want quotes to be part of your results, just use "(?:""|[^"])*".

See demo.

Another "unrolled" regex for this task:

"((?:[^"]*(?:""[^"]*)*))"

1 Comment

Unrolled version is 43% faster than the one with alternatives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.