-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...

Community
  • 1
  • 1
iOnline247
  • 126
  • 7
  • What are you trying to actually do here? – hugomg Nov 29 '11 at 01: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 Nov 29 '11 at 01: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 Nov 29 '11 at 03:19
  • Here's the answer: val.match( /^[\/|_]|[~"#%&*:<>?\\{|}]|[\/]{2,}|[\/|.]$/ ) – iOnline247 Nov 29 '11 at 03: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 Nov 29 '11 at 04:09
  • I couldn't last night due to the point levels I had. – iOnline247 Nov 29 '11 at 13:13

5 Answers5

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.
hugomg
  • 68,213
  • 24
  • 160
  • 246
2

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

Blender
  • 289,723
  • 53
  • 439
  • 496
  • To see if this worked, I changed my code to: val.match( /[\/]+/ ) and it matches on the first "/". Any other ideas? – iOnline247 Nov 29 '11 at 02: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.

hugomg
  • 68,213
  • 24
  • 160
  • 246
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 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 Nov 29 '11 at 02:24
0

Why not use indexof? That would be simpler.

Richard Brightwell
  • 3,012
  • 2
  • 20
  • 22
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,}|[~"#%&*:<>?\\{|}]|[\/|.]$/ )
iOnline247
  • 126
  • 7
  • I'm quite sure `[\/|_]` matches more than you think it does. – Toto Nov 29 '11 at 13:41
  • Your regex matches a string that contains only one char of `/_~"#%&*:<>?\\{}|.` or 2 or more `/`. – Toto Nov 29 '11 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 Nov 29 '11 at 14:40