question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect quote string matching

See original GitHub issue

Overview

The quotation string matching system matches incorrectly for adjacent strings.

Example

const nlp = require('compromise');

// Try splitting the string into quoted strings.
console.log(
    nlp('My "String" "with many" adjacent "nested" \'quotes\'')
    .quotations()
    .out('array')
)

Output

[
    "string with many",
    "nested quotes"
]

Expected

[
    "string",
    "with many",
    "nested",
    "quotes"
]

Workaround

So, I solved this for my specific circumstance like so:

function quotes(string) {
    // Match a single word surrounded by (")
    return string.match(
        '/^"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"$/'
    )
    
    // Match a single word surrounded by (')
    .concat(string.match(
        '/^\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'$/'
    ))
    
    
    // Match a two or more word surrounded by (")
    .concat(string.match(
        '/^"[^"\\\\]*(?:\\\\.[^"\\\\]*)*$/ ' +
        '/[^"\\\\]*(?:\\\\.[^"\\\\]*)*/+? ' +
        '/^[^"\\\\]*(?:\\\\.[^"\\\\]*)*"$/'
    ))
    
    // Match a two or more word surrounded by (')
    .concat(string.match(
        '/^\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*$/ ' +
        '/[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*/+? ' +
        '/^[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'$/'
    ));
}

Using the RegExp by Jeffrey Friedl provided by ridgerunner on stackoverflow

Edit 1 I will note my solution is not perfect as it outputs:

[
    "string",
    "nested",
    "quotes",
    "with many"
]

End: Edit 1

A side note: The regex on the match syntax wiki page should be: /rain(ing|ed)/ meaning match rain and (ing or ed). The current /rain[ing|ed]/ means match rain and (i, n, g, | e or d).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
spencermountaincommented, Feb 26, 2018

ah, thanks Sca. This is a great bug. and your workaround is very clever. i think i’ve fixed this in 11.5.2 - at least your example works. Can you take a look? There’s all sorts of issues with, unicode quoations, um and the trailing possessive apostrophe inside a quote, which might trip it still. cheers

0reactions
spencermountaincommented, Jul 13, 2018

hey, yeah thanks @Errogant. I’ve made a new issue here

Read more comments on GitHub >

github_iconTop Results From Across the Web

️‍ Incorrect Quote Generator🏳️‍ - Perchance.org
Names (including extra ones) will be selected from and placed randomly into the quotes. NOTE: Disabling NSFW only disables dirty jokes. It does...
Read more >
Regex for quoted string with escaping quotes - Stack Overflow
Explanation: Every quoted string starts with Char: " ;; It may contain any number of any characters: .*? {Lazy match}; ending with non...
Read more >
Solved: Compare String Starts With double quotes
Solved: Hi , I have to check in condition if a response content starts with double quote . How ever proxy is not...
Read more >
'Single' vs "Double" quotes for strings in javascript - Flexiple
We can fix this by using the fact that javascript allows both single and double quotes to define a string.
Read more >
grep: Pattern Matching and Replacement - Rdrr.io
character to a character vector. Long vectors are supported. ignore.case. if FALSE , the pattern matching is case sensitive and if TRUE ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found