1

I can't get those RegEx pattern to work with JavaScript (in PHP they work):

/^[^\/home\/]/
/^\/home\/$/
/^\/home\/.+\/.+$/

I want to allow input like this:

/home/username

What I don't want is:

/home/
/username
/home/username/info

Here is my code:

var regex1 = /^[^\/home\/]/;
var regex2 = /^\/home\/$/;
var regex3 = /^\/home\/.+\/.+$/;
if( regex1.test(homedir)
|| regex2.test(homedir)
|| regex3.test(homedir) ) { ... }

I have no errors in the console.

Could someone explain to me why it is not working in JavaScript?

EDIT

I changed it to /^\/home\/[A-Za-z0-9]+$/ and now it's working:

var rgx = /^\/home\/[A-Za-z0-9]+$/;
if( !rgx.test(homedir) ) { ... }
manifestor
  • 1,352
  • 6
  • 19
  • 34
  • 2
    plz post your input string and expected output – A l w a y s S u n n y Apr 30 '18 at 17:58
  • 1
    "why it is not working in JavaScript" could you tell what is not working? there's some errors in the console? are you trying to replace or to find something with this regex? Please, be more specific – Calvin Nunes Apr 30 '18 at 17:58
  • https://stackoverflow.com/questions/34134741/what-is-the-difference-between-php-regex-and-javascript-regex?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Gino Pane Apr 30 '18 at 17:59
  • 1
    Possible duplicate of [what is the difference between PHP regex and javascript regex](https://stackoverflow.com/questions/34134741/what-is-the-difference-between-php-regex-and-javascript-regex) – Gino Pane Apr 30 '18 at 18:00
  • This `^[^/home/]` won't match `/homd` or `/anything` –  Apr 30 '18 at 18:04
  • This `^/home/$` only matches `/home/` –  Apr 30 '18 at 18:06
  • This `^/home/.+/.+$` only matches `/home/` with trailing chars with a / sandwiched in between. –  Apr 30 '18 at 18:08
  • Your premise is based on _what you don't want_, not the other way around. You should tailor the expression to meet that premise. `^(?!(?:/home/(?:username/info)?|/username)$).+$` –  Apr 30 '18 at 18:11
  • Otherwise, the validation is based on too little information. –  Apr 30 '18 at 18:17
  • 1
    Thank you very much @sln - yes, I agree to the fact that I maybe have to rewrite it. I Just wanted to understand the difference as in PHP it's working. – manifestor Apr 30 '18 at 18:19
  • 1
    I rewrote it to `/^\/home\/[A-Za-z0-9]+$/` and it's working fine as I now check if the string matches instead of checking if it doesn't match, just as @sln suggested. Thank you all for you help :) – manifestor Apr 30 '18 at 18:38
  • None of the three regexps work in PHP as they do not match your `/home/username` string. – Wiktor Stribiżew Apr 30 '18 at 19:13
  • This is because I checked the opposite first (if the entered string does NOT match `/home/username`), and then I changed it, after sln's suggestion. They work very well in PHP if you check it the other way round as I had it in the beginning. – manifestor Apr 30 '18 at 19:46
  • Possible duplicate of [Converting PHP Regex's to Javascript (or what are the differences?)](https://stackoverflow.com/questions/1131918/converting-php-regexs-to-javascript-or-what-are-the-differences) – wp78de Apr 30 '18 at 23:39

1 Answers1

0

Remove the string start Regex parameter ^

You will now have: \/home\/.+\/.+$

Example built here showing validation: Regex Home URL example

NSTuttle
  • 3,237
  • 2
  • 17
  • 26