-1

I'm trying to test if the / character is inside double quotes within a JavaScript string. For instance, "hello/world" will match, whereas hello/world/"How are you" will fail because it's outside of the double quotes. I've been looking around and haven't found an answer to this.

Also, "hello"world/how"are you" should fail as the opening and closing double quotes do not encapsulate the /, as determined from left to right. To reiterate, I want to match all instances within/inside/in between closing and opening double quotes, which is unlike Alternative to regex: match all instances not inside quotes

Community
  • 1
  • 1
FullStack
  • 5,902
  • 4
  • 43
  • 77
  • 1
    What should the result be for `"hello"world/how"are you"`? – Barmar Jul 26 '15 at 06:41
  • @Barmar great question. That should pass as the opening and closing double quotes are not encapsulating the `/`, as determined from left to right – FullStack Jul 26 '15 at 06:42
  • You should clarify that in the question, since several of the answers get it wrong. – Barmar Jul 26 '15 at 06:44
  • I think there's confusion about "matching", "failing" and "passing". I have edited my question to be more clear. – FullStack Jul 26 '15 at 06:55

4 Answers4

2

I think you can use this:

/^(((\"[^"\/]*\")\/?)|([^"\/]*\/?))+$/igm

or

/^(\"[^"\/]*\"|[^"\/]*\/?)+$/im

[Regex Demo]

shA.t
  • 16,580
  • 5
  • 54
  • 111
-1
str = 'hello/world"how are you"';

q1 = str.indexOf('"');
q2 = str.lastindexof('"');
slsh = str.indexOf("/");
if( slsh > q1 && slsh <q2){

    dosomething;
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
-1

Without using regex's an algorithm would be something like... Please test yourself!

 Startquotehit = false
    insidequote = false
    Gotaslash = false
    Wantedresult = false

    For x to.. String.length
    {
    If x == quote
    {
     Startquotehit = true;
     Insidequote = !insidequote;
     If gotaslash == true && insidequote
         Wantedresult = true;
    }     

    If x == slash
    {
      If startquotehit
         Gotaslash = true;
    }
 }

Wantedresult would tell you if the slash was quoted 'inside quotes' to cover your false scenario of "....".../..."..."

Hace
  • 1,421
  • 12
  • 17
-2
/".*\/.*"/.test(your_string)

See JavaScript regular expressions' test method.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Vidul
  • 10,128
  • 2
  • 18
  • 20