Finding + – && || ! ( ) { } [ ] ^ " ~ * ? : / in a string and adding before it [duplicate]

  javascript, regex, solr

Im looking for a quick solution to meet this criteria in javascript:

Escaping Special Characters
Lucene supports escaping special characters that are part of the query syntax. The current list special characters are

+ - && || ! ( ) { } [ ] ^ " ~ * ? :  /

To escape these character use the before the character. For example to search for (1+1):2 use the query:

(1+1):2

I have been doing this with spaces via values.map(t => t.replace(/s/g, ' ')). Is there a way to incorporate all of those characters in the replace function? Mine hard codes a a escape ” but that doesn’t see applicable here.

Source: Ask Javascript Questions

LEAVE A COMMENT