How to ignore brackets in a regex

kish-an

I have a regex that takes a template literal and then matches it against a CSV of conditions and links.

const regex = new RegExp(`^${condition},\/.+`, 'gi');

For example, the variable Sore throat would match

'Sore throat,/conditions/sore-throat/'

I've come across an issue where the template literal might contain brackets and therefore the regex no longer matches. So Diabetes (type 1) doesn't match

'Diabetes (type 1),/conditions/type-1-diabetes/'

I've tried removing the brackets and it's contents from the template literal but there are some cases where the brackets aren't always at the end of the string. Such as, Lactate dehydrogenase (LDH) test

'Lactate dehydrogenase (LDH) test,/conditions/ldh-test/'

I'm not too familiar with regex so apologies if this is simple but I haven't been able to find a way to escape the brackets without knowing exactly where they will be in the string, which in my case isn't possible.

Dan

You are trying to use a variable that might contain special characters as part of a regex string, but you /don't/ want those special characters to be interpreted using their "regex" meaning. I'm not aware of any native way to do this in Javascript regex - in Perl, you would use \Q${condition}\E, but that doesn't seem to be supported.

Instead, you should escape your condition variable before passing it into the regex, using a function like this one:

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Regex: How to ignore dots in connected words

分類Dev

how can I ignore nested patterns with regex

分類Dev

How can I ignore the first line break for a Positive Lookahead (regex)

分類Dev

Split string by comma but ignore commas in brackets or in quotes

分類Dev

Make Microsoft Word Spellcheck Ignore Square Brackets?

分類Dev

Regex to match text, but not if contained in brackets

分類Dev

Regex for removing Commas within brackets

分類Dev

javascript regex skip square brackets

分類Dev

Regex Curly brackets not working properly

分類Dev

Regex removing ignore chars

分類Dev

How to change parentheses to brackets

分類Dev

Python Regex To Ignore Date Pattern

分類Dev

C# Regex Atomic Opening and Closing Brackets

分類Dev

Regex : replace quotes except inside curly brackets

分類Dev

How to add [ ] brackets to JSON in JavaScript

分類Dev

javascript regex to ignore sub directories for chokidar

分類Dev

Regex Ignore First 6 Matches Of Character

分類Dev

gsub regex in R - ignore newline symbol

分類Dev

regex to ignore a character in between two string?

分類Dev

Javascript Regex: ignore subgroup when matching

分類Dev

how to ignore a RUN test

分類Dev

Using Regex to get string between lookbehind and lookahead with brackets and parenthesis in it

分類Dev

Remove Square Brackets and Line Breaks in String with regex in R

分類Dev

Regex to match pipes not within brackets or braces with nested blocks

分類Dev

Regex match last square brackets in SQL Script SSMS

分類Dev

Python regex find everything inside curly brackets after a certain string

分類Dev

Regex to match an the value of a json entry that may contains square brackets []

分類Dev

using regex to remove the opening brackets of JSON data coldfusion

分類Dev

How to delete matching brackets in VS Code?

Related 関連記事

  1. 1

    Regex: How to ignore dots in connected words

  2. 2

    how can I ignore nested patterns with regex

  3. 3

    How can I ignore the first line break for a Positive Lookahead (regex)

  4. 4

    Split string by comma but ignore commas in brackets or in quotes

  5. 5

    Make Microsoft Word Spellcheck Ignore Square Brackets?

  6. 6

    Regex to match text, but not if contained in brackets

  7. 7

    Regex for removing Commas within brackets

  8. 8

    javascript regex skip square brackets

  9. 9

    Regex Curly brackets not working properly

  10. 10

    Regex removing ignore chars

  11. 11

    How to change parentheses to brackets

  12. 12

    Python Regex To Ignore Date Pattern

  13. 13

    C# Regex Atomic Opening and Closing Brackets

  14. 14

    Regex : replace quotes except inside curly brackets

  15. 15

    How to add [ ] brackets to JSON in JavaScript

  16. 16

    javascript regex to ignore sub directories for chokidar

  17. 17

    Regex Ignore First 6 Matches Of Character

  18. 18

    gsub regex in R - ignore newline symbol

  19. 19

    regex to ignore a character in between two string?

  20. 20

    Javascript Regex: ignore subgroup when matching

  21. 21

    how to ignore a RUN test

  22. 22

    Using Regex to get string between lookbehind and lookahead with brackets and parenthesis in it

  23. 23

    Remove Square Brackets and Line Breaks in String with regex in R

  24. 24

    Regex to match pipes not within brackets or braces with nested blocks

  25. 25

    Regex match last square brackets in SQL Script SSMS

  26. 26

    Python regex find everything inside curly brackets after a certain string

  27. 27

    Regex to match an the value of a json entry that may contains square brackets []

  28. 28

    using regex to remove the opening brackets of JSON data coldfusion

  29. 29

    How to delete matching brackets in VS Code?

ホットタグ

アーカイブ