-2

I gave it a college try, but I'm stumped. I'm trying to find consecutive slashes within a string. The rest of the regex works great, but the last part I can't quite get.

Here's what I have:

val.match( /^[\/]|[~"#%&*:<>?\\{|}]|[\/|.]$/ ) 

and finding this thread, I decided to update my code to no avail: RegEx to find two or more consecutive chars

val.match( /^[\/]|[\/]{2,}|[~"#%&*:<>?\\{|}]|[\/|.]$/ ) 

What do I need to get this thing going?

So, I need this regex to look for many characters. That would explain the first code sample that I provided:

val.match( /^[\/]|[~"#%&*:<>?\\{|}]|[\/|.]$/ ) 

What I need it to also do, is look in the string for a double whack. Yes, I'm well aware of indexOf and other string manipulation techniques, but I labeled it regex because it needs to be. Let me know if you need more info...

6
  • What are you trying to actually do here?
    – hugomg
    Commented Nov 29, 2011 at 1:43
  • i'm not very understand what your problem is. but, if you want to match two consecutive '//', you can simple.match(/^\/{2}/);, not sure about the [~"#%&*:<>?\\{|}] part anyway... could you explain more?
    – Ya Zhuang
    Commented Nov 29, 2011 at 1:47
  • 1
    I still don't understand what you are trying to do. Your question title talks about consecutive slashes, your actual regex contains a bunch of other stuff, and you link to a question about two consecutive alpha characters. Can you give an example of your input data and required output so we don't have to guess based on your regex (given you've said that regex doesn't work anyway)?
    – nnnnnn
    Commented Nov 29, 2011 at 3:19
  • Here's the answer: val.match( /^[\/|_]|[~"#%&*:<>?\\{|}]|[\/]{2,}|[\/|.]$/ )
    – iOnline247
    Commented Nov 29, 2011 at 3:40
  • If that's the answer to whatever your question was (I still don't get it) then post it as an answer and accept it, don't put it as a comment. Or accept the existing answer that most helped you.
    – nnnnnn
    Commented Nov 29, 2011 at 4:09

5 Answers 5

3

Uh, why aren't you just doing

/\/{2,}/g

? Your regexes in the OP seem way more complicated...

  • \/ matches a literal backslash character
  • {2,} tells to match it twice or more
  • /g makes the pattern global so you can find all occurences of the pattern in your strings.
0
2

[\/]+ should match one or more /s.

1
  • To see if this worked, I changed my code to: val.match( /[\/]+/ ) and it matches on the first "/". Any other ideas?
    – iOnline247
    Commented Nov 29, 2011 at 2:16
1
/(.)$1+/

would find any place where a single character occurs 2 or more times. the (.) matches a single character, and captures that character into $1, which you then require to be immediately after the initial character, 1 or more times.

For slashes, you can simplify it down to

/\/{2,}/
/\/\/+/

but then you're into leaning toothpick territory.

1
  • I was pretty amped when I saw that this worked: val.match( /\/{2,}/ ) but adding it in makes it match on the first whack. I'm sure it's something I'm doing, so if you could clarify... val.match( /^[\/]|\/{2,}|[~"#%&*:<>?\\{|}]|[\/|.]$/ )
    – iOnline247
    Commented Nov 29, 2011 at 2:24
0

Why not use indexof? That would be simpler.

0
0

Here's the answer.

val.match( /^[\/|_]|[~"#%&*:<>?\\{|}]|[\/]{2,}|[\/|.]$/ )

Not sure why the other version doesn't work, but maybe someone could shed some light onto the matter.

Tests:

  1. _text - Failed leading underscore
  2. /text - Falied leading whack
  3. text~moreText - Failed contains invalid character: ~"#%&*:<>?\{|}
  4. text//text - Failed double whack
  5. text/ - Failed trailing whack
  6. text. - Failed trailing period

Not sure why the code below wasn't working, but moved the double whack test and it works now:

val.match( /^[\/|_]|[\/]{2,}|[~"#%&*:<>?\\{|}]|[\/|.]$/ )
3
  • I'm quite sure [\/|_] matches more than you think it does.
    – Toto
    Commented Nov 29, 2011 at 13:41
  • Your regex matches a string that contains only one char of /_~"#%&*:<>?\\{}|. or 2 or more /.
    – Toto
    Commented Nov 29, 2011 at 13:43
  • I don't need more than finding one of those chars. If one of those chars are typed, certain controls are locked. ^[\/|_] seems to be working as I expect it to. This piece should only find a / or _ at the beginning of the string. Are you getting different results?
    – iOnline247
    Commented Nov 29, 2011 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.