Given three URLS:
http://stackoverflow.com/questionsss/3003310 -> true
http://stackoverflow.com/questionsss/3003310?s=1 -> false (s=1 found)
http://stackoverflow.com/questionsss?s=1&N=3003310 -> false (s=1 found)
How can I write a Regex in JS that returns true if 3003310 is found in the URL and false if s=1 is found anywhere in the URL.
Thanks!
maybe something like this:
^(?=.*3003310)(?!.*\bs=1\b).*
a positive lookahead, and a negative one, for each condition. the \b word boundaries around s=1 prevent false positives such as sass=1 or s=100.
REGEXP:
(.*\?s=1.*)
u should do a method, if result is equal to regex match. it will return FALSE else TRUE.
Original Text:
http://stackoverflow.com/questionsss/3003310 -> true
http://stackoverflow.com/questionsss/3003310?s=1 -> false (s=1 found)
http://stackoverflow.com/questionsss?s=1&N=3003310 -> false (s=1 found)
Result:
http://stackoverflow.com/questionsss/3003310?s=1 -> false (s=1 found)
http://stackoverflow.com/questionsss?s=1&N=3003310 -> false (s=1 found)
Algorithm:
Print(!OriginalText.equals(MethodCheck(Result)) // if empty. it will be TRUE
See: https://regex101.com/r/335HNW/1
Related
In tickscript the norwal way to do if else is in the following:
if(condition, true expression, false expression)
However, for the false expression, I want my code to do nothing as in the following:
if(condition, true print("that is true"), false -do nothing- )
I already tried putting some blank and/or deleting false expression part but it did not work.
Is there a way to do that in tickscript?
This question already has answers here:
Why is a Regexp object considered to be "falsy" in Ruby?
(2 answers)
Closed 2 years ago.
In Ruby, only false and nil are falsey; everything else is truthy. You can use two not operators to check an object's truthiness:
!!false # false
!!nil # false
!![] # true
!!{} # true
!!'' # true
!!0 # true
But then I found that empty-regex literal // is falsey, but as a variable, it's truthy!:
!!// # false!
not not // # false
x = //
x.class # Regex
!!x # true
I think this is a quirk of the parser. How can I ask the parser what it's doing?
This is not just applicable to empty regex literal, but rather all regex literals;
!!/(.*)/
=> false
x = /(.*)/
!!x
=> true
At first this appeared to be an issue with the way the regexp is being constructed
!!Regexp.new('')
=> true
x = Regexp.new('')
!!x
=> true
!!//.freeze
=> true
But digging deeper, this appears to be the cause:
!!//
=> false
$_ = 'b'
!!//
=> true
Setting '$_' back to nil will reset the outcome. The value for $_ is set from Kernel.readline or Kernel.get siblings. In a new console:
Kernel.readline
a
"a\n"
!!//
=> true
So instantiating a Regexp object with // appears to match it against the value of $_
To be honest, I'm not 100% sure, but my idea is that Ruby is doing the next thing when you run !!x
puts !!// # this is doing a phantom .match(nil), like below, so, returns false
x = //
puts !!x.match(nil)
I have a regex expression as follows:
return ( ! preg_match("/^([-a-z_ -])+$/i", $str)) ? FALSE : TRUE;
I need to add to it to allow a period, so that the input could be something like this:
"st. austell" or "st. thomas"
I have tried various ways to add the period to the above rule, but the page either crashes out and just displays a blank page, or my validation errors are triggered.
I have tried things like this, but to no avail.
([-a-z_ -.])
([-a-z_ -\.])
([-a-z_ -])\.
(\.[-a-z_ -]) etc etc...
I have tried everything and don't seem to bee having any luck - any ideas
Many Thanks
James
Three points:
The initial regex is redundant, since it specifies - twice.
[a-z_ .-] should work, you just have to let a-z together and - just after [ or just before ] (otherwise, - will be considered as a metachar).
return !condition ? false : true; should be simplified to return condition;, since condition already returns true or false (same logic for if(!condition) {var = false;} else {var = true;} which should be simplified to var = condition;).
I'm trying to validate an url without domain, just the path and params.
The regular expression that I'm using do most of the work, but It has some errors that I dont know how to prevent (I'm pretty noob with regexp):
/^(\/([\w#!:.?+=&%#!\-\/])+)$/i
The next example are correctly validated
/asd.jsp -> true
/asd/asd.jsp -> true
/asd/asd.jsp?bar=baz&inga=42&quux -> true
/asd/asd.jsp?bar=ba z&inga=42&quux -> false
But this ones arent correct ulrs and them gives me true too:
/asd/asd./jsp -> true :(
/asd/asd.jsp/ -> true :(
/asd./asd.jsp -> true :(
/asd///asd.jsp -> true :(
/asd/asd.jsp&bar=baz?inga=42?quux -> true :(
Do you recommend to use a function instead of a regex?
Very much thanks!
Try this:
^(\\/\\w+)+\\.\\w+(\\?(\\w+=[\\w\\d]+(&\\w+=[\\w\\d]+)+)+)*$
I already escaped special characters, so you can directly use it in java.
By the way, /asd/asd.jsp?bar=baz&inga=42&quux is not a valid URL.
Unescaped Regex:
^(\/\w+)+\.\w+(\?(\w+=[\w\d]+(&\w+=[\w\d]+)+)+)*$
^(\/\w+)+(\.)?\w+(\?(\w+=[\w\d]+(&\w+=[\w\d]+)*)+){0,1}$
VALID:
/test
/test/test
/test.jsp
/test.jsp?v=1
/test.jsp?v=1&v=2
/test/test.jsp?v=1&v=2
INVALID:
/test/test./jsp
/test/test.jsp/
/test./test.jsp
/test///test.jsp
Using nodeunit is there an assert to check for false values? Other testing frameworks have something like assertFalse, should I use something like:
test.ok(!shouldBeFalse());
or
test.equals(shouldBeFalse(), false);
Or is there a project that adds a false assertion?
If you want to be sure only boolean false is matched than use strictEqual:
test.strictEqual(shouldBeFalse(), false)
Otherwise equal is ok too.