Skip to main content
added 92 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

but to me that seems like overkill and makes the code a little bit harder to understand.

but to me that seems like overkill and makes the code a little bit harder to understand.

added 45 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

I'm not sure but I think you may be asking how to both protect against bash globbing, word splitting, etc. constructs and ERE metachars so you can create a regexp that includes parts to be treated literally. If so:

I'm not sure but I think you may be asking how to both protect against bash globbing, etc. constructs and ERE metachars so you can create a regexp that includes parts to be treated literally. If so:

I'm not sure but I think you may be asking how to both protect against bash globbing, word splitting, etc. constructs and ERE metachars so you can create a regexp that includes parts to be treated literally. If so:

added 1090 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

In the js example in your question if (/[0-9]+' and [0-9]+\$price/... - that regexp is identical to the one you'd store in a variable for bash except that in bash we use ' (or ") as the delimiter and so can't use ' (or ") in the string as-is while js is using / as the delimiter and so, I suspect, you couldn't use / in the string as-is and so the js regexp isn't actually any more literal than the bash regexp, it just uses different delimiters and doesn't need to be stored in a variable.

If you don't want to use a variable in bash, you can always just write a function to do the comparison:

$ cat tst.sh
#!/usr/bin/env bash

cmpr() { [[ "$1" =~ $2 ]]; }

text='The pizza is 2'\'' and 100$price' # The pizza is 2' and 100$price

if cmpr "$text" '[0-9]+'\'' and [0-9]+\$price'; then
    echo "this is what I prefer to have -- literal regex, like / /g in js. But this wont even compile"
fi

$ ./tst.sh
this is what I prefer to have -- literal regex, like / /g in js. But this wont even compile

In the js example in your question if (/[0-9]+' and [0-9]+\$price/... - that regexp is identical to the one you'd store in a variable for bash except that in bash we use ' (or ") as the delimiter and so can't use ' (or ") in the string as-is while js is using / as the delimiter and so, I suspect, you couldn't use / in the string as-is and so the js regexp isn't actually any more literal than the bash regexp, it just uses different delimiters and doesn't need to be stored in a variable.

If you don't want to use a variable in bash, you can always just write a function to do the comparison:

$ cat tst.sh
#!/usr/bin/env bash

cmpr() { [[ "$1" =~ $2 ]]; }

text='The pizza is 2'\'' and 100$price' # The pizza is 2' and 100$price

if cmpr "$text" '[0-9]+'\'' and [0-9]+\$price'; then
    echo "this is what I prefer to have -- literal regex, like / /g in js. But this wont even compile"
fi

$ ./tst.sh
this is what I prefer to have -- literal regex, like / /g in js. But this wont even compile
added 497 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 497 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 497 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 497 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 50 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading