-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is Conditional Regex Supported? #22
Comments
@afshindavoudy Can you give an example? I haven't found any doc about conditional constructs in the MDN JavaScript Regex pages |
@rioj7 I need to implement a conditional regex as follows: |
@afshindavoudy does this work
|
I've set it up as a keybinding for execution through the "Select By" extension, as shown below: {
"key": "alt+j", // Keybinding
"when": "editorTextFocus",
"command": "moveby.regex", //Run by "Select By" extension
"args": {
"regex": "('[^']*'|[^-]*-)",
"properties": [
"next",
"start",
]
},
}, However, it currently matches all hyphens and does not skip those within single quotes (' '). For example, in the text
it matches all hyphens, including the ones within the single quotes. The regular expression I'm attempting to create is more complex than the previous example I provided. I believe it can only be achieved using a conditional regex. |
Can you give more detailed examples of where the cursor is and where you expect the cursor to move-to after the command.
Where is this quote relative to the cursor position? Can you describe what you actually want to search/move? In your example
you use Lookahead and Lookbehind, they are supported by JavaScript. Only fixed length Lookbehind. Do a web search for |
@rioj7
Descriptions:
Captures any closing brackets
Captures any
Captures the character right before the above character (The closing "'` itself) This solution functions perfectly in most scenarios. However, it also captures characters like "-", "+", "&," "%", and so on, when they appear within quotations("", '', ``) and comments. This is the issue I am currently addressing (and trying to fix with conditional patterns). |
By the way, I've encountered two more challenges: I need to incorporate regex patterns to detect "end of line" and "The first empty line between brackets" . I've tried using the following patterns:
However, these patterns interfere with the ability to navigate to the next line when they are detected. |
You have the word
I just tried this in a text file (using the Find box of VSC, no need to escape) and I'm amazed that a variable length Negative Lookbehind works.
This is equivalent with What I don't get is the logic of a LookBehind INSIDE a LookAhead
The whole expression matches a character (first Using this explanation an equivalent regex is: You don't want to match the
The regex that matches the closing It checks if it is inside a string first, then try find next string, then try The closing If you are inside a string the whole string content and closing I have to modify the extension to make the following properties working: {
"key": "alt+enter",
"when": "editorTextFocus",
"command": "moveby.regex",
"args": {
"regex": "....",
"properties": ["next", "start"],
"groups": [1]
},
}, It checks if group 1 has a match and uses that to determine the
In case of Comments can be eliminated if you start the regex with: Regarding the false positives inside strings, how often does that happen and how bad is it to hit the key a second time to jump to the "next" positive or false hit.
Have you looked at the To get it very smooth you have to use an AST or you might be helped with the TextMateScope of the cursor position to determine what to do, use which regex, based on the fact if the cursor is in a string or comment or other. |
Hi @rioj7, First, I want to acknowledge the typo you spotted in my regex comment – you're absolutely right; "was" was indeed a typo generated by auto-completion. Regarding the regex pattern discussion: Your explanation of the logic behind I'm also excited about the potential modification of the extension to enhance its functionality. It would be a fantastic addition, and I'm looking forward to giving it a try once it's implemented. Please keep me updated on its progress. Your other tips and suggestions are invaluable, and I'm keen to incorporate them into my existing regex pattern. Thanks once again for your assistance and guidance. Please feel free to share any further advice or insights you may have. |
Hi,
I appreciate your excellent extension! However, I'm having trouble getting a conditional regex to work. I'd like to confirm if this plugin supports conditional regex.
The text was updated successfully, but these errors were encountered: