Skip to content
the poor coder

regex find spaces in double quotes

Poor Man
Published
Reading time
1 min read
On this page

Regex Find Spaces in Double Quotes#

When working with large amounts of data, it can be helpful to use regular expressions to find specific patterns. One common task is to find spaces within double quotes. Here are a few ways to accomplish this:

Method 1: Using Lookarounds#

One way to find spaces within double quotes is by using a positive lookbehind and lookahead:

REGEX

(?<=\")[^\"]*\s[^\"]*(?=\")

This regex pattern searches for any space character (represented by the \s character class), located between two sets of non-double-quote characters (represented by [^\"]*). The positive lookbehind (?<=\") and lookahead (?=\") ensure that the pattern is only matched if it appears within double quotes.

Method 2: Using Capture Groups#

Another way to find spaces within double quotes is by using capture groups:

REGEX

\"([^\"]*\s[^\"]*)\"

This regex pattern captures any sequence of non-double-quote characters, followed by a space, followed by another sequence of non-double-quote characters, as long as the entire sequence is enclosed in double quotes.

Method 3: Using Alternation#

A third method for finding spaces within double quotes is by using alternation:

REGEX

\"[^\"\s]*\s[^\"\s]*\"

This regex pattern matches any sequence of non-double-quote, non-space characters, followed by a space, followed by another sequence of non-double-quote, non-space characters, as long as the entire sequence is enclosed in double quotes.

No matter which method you choose, regular expressions can be a powerful tool for finding and manipulating text data.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC