-1

If I want to find a reference to precisely the following string:

http ://www.mydomain.com/home 

within a more complex regex expression. Is it possible to escape the whole sequence instead of escaping each / and . character individually? To get something more readable than

/http:\/\/www\.mydomain\.com\/home/

In the regex parsing site https://regexr.com/ , if I type the url in and set a regex to

/(http ://www.mydomain.com/home)/

, it appears to recognize the string, yet declares an error:

Unescaped forward slash. This may cause issues if copying/pasting this expression into code. 

So I'm confused about this issue.

Francis
  • 702
  • 9
  • 21
  • 1
    You want to have a multiline regex expression? Or am I understanding the question wrong? – Icepickle Feb 25 '19 at 08:23
  • See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp, you can match multiple lines using: `myString.match(/.*/, 'm')` – TheChetan Feb 25 '19 at 08:26
  • What backslashes have you got and which ones do you want to get rid of? What does regex have to do with this? – Wiktor Stribiżew Feb 25 '19 at 08:27
  • @TheChetan You wanted to write `myString.match(/.+/g)` I believe. [`match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Syntax) only accepts a single argument, a RegExp object. – Wiktor Stribiżew Feb 25 '19 at 08:30
  • edited the question, as it appears not to have been understood. – Francis Feb 26 '19 at 10:17
  • Unfortunately, you can't really do this in JS. In Java and Perl, you can use the `\Q` and `\E` metacharacters. However, you can't really do that in JS (https://stackoverflow.com/q/6318710/4851565). However, there is this nifty answer on escaping regexes in JS (https://stackoverflow.com/q/3446170/4851565). – entpnerd Feb 27 '19 at 05:49
  • thanks @entpnerd, that answers the question – Francis Feb 27 '19 at 08:27
  • To all: I believe negative votes are due to misunderstanding of the question, that led to being characterized as unsomething (unappropriate? unethical? I forget). The anathema has been lifted since, but the negative votes remain. Oh well. – Francis Feb 27 '19 at 08:34
  • @Francis I found this question in the closed queue. The consensus was that the question was unclear. However, based on your edit, it was made more clear and was reopened. – entpnerd Feb 27 '19 at 15:25
  • Please someone reclose as a duplicate of [Is there a RegExp.escape function in Javascript?](https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript). BTW, if it is not about escaping special chars it is no any clearer than it was before reopening. – Wiktor Stribiżew Feb 28 '19 at 08:55
  • I don't think this is a duplicate of https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript, unless I misunderstood. What I understand of that post is that he wants to take a string and transform that into a regex expression. What I want to do is to prevent regex from parsing one part of the expression. – Francis Feb 28 '19 at 09:16
  • Investigating further, this question seems to give the answer: https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex. https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – Francis Feb 28 '19 at 09:24
  • I had trouble reading https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript because it concentrates on the how rather than the what – Francis Feb 28 '19 at 09:25
  • What I was looking for was a simpler solution, one that would use regex syntax, that would not oblige me to concatenate a string, then create a new regex object. It appears that doesn't exist. I don't understand the issue people have with the question. – Francis Feb 28 '19 at 09:31

1 Answers1

-1

It appears that regex does not offer such a syntax, at least for Javascript. It is possible, however, to proceed as follows:

  1. use a string and automatically escape all the special characters in it, as indicated here: Javascript regular expression - string to RegEx object
  2. concatenate that string with strings representing the rest of the expression you want to create
  3. transform the string into a regex expression as indicated in Escape string for use in Javascript regex .
Francis
  • 702
  • 9
  • 21