0

Say I have a RegEx like the following:

^[a-zA-Z]\w{12}$

And I have the following string:

%7AgTy!5hG^vxWa2@AgW

I would like to "pull" out of that string something that conforms to that regex. In this example we would get the following:

AgTy5hGvxWa2A

Reason: it starts with A because the regex says the first letter must be [a-zA-Z] (so it skips the first 2 characters), and then it pulls successive \ws out until it reaches 12 characters.

Is this sort of thing possible?

Edit: My apologies for being unclear. I'm not looking for a new regular expression that will give the proper output. Rather, I'm looking for a way to use the existing RegEx to extract the proper output. In my program these regular expressions are entered by hand by the user to extract a password from a long base256 hash such that it will conform to these existing password requirement regexes.

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • No, it's not. You don't make the regex then write a string that should match. It's the other way around. Your regex doesn't match anything at all from that string. –  Feb 09 '18 at 16:36
  • Well, you can't use the existing regex because it doesn't work. It's as simple as that. Not sure what you're looking for exactly. You can't edit a regex to make it work without editing the regex? – ctwheels Feb 09 '18 at 16:39
  • I'm looking for a way to use existing password rule regexes to extract a password from a long string of characters such that the resulting password conforms to the regex rule. – Ryan Peschel Feb 09 '18 at 16:40
  • @RyanPeschel I'm really confused by your comment above. What do you mean you're trying to validate passwords? Don't do it however you're doing it. You're not making your passwords stronger, but actually making them less secure. Please see [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/48346033#48346033) for more information. – ctwheels Feb 09 '18 at 17:13
  • For doing what you want to do, you would need to write a regex parser. So ... forget it :-) – trincot Feb 09 '18 at 18:11

1 Answers1

0

Instead of trying to match what you want and reconstructing the string, replace everything you don't want with nothing. This gives the impression that you're extracting what you need, but, in fact, it's doing the opposite; gets rid of everything you don't want to extract. I also dropped $ from the end of your original pattern otherwise it'll never match the string you present in your question.

See regex in use here

^[^a-z]+|\W+
  • ^ Assert position at the start of the line
  • [^a-z]+ Matches any character that is not in the range a-z one or more times. Since the i flag is specified, this also matches A-Z
  • \W+ Match any non-word character one or more times

const regex = /^[^a-z]+|\W+/gi
const a = [
  `%7AgTy!5hG^vxWa2@AgW`,
  `%7AgTy!5hG^vxWa2@`
]

a.forEach(function(s) {
  var clean = s.replace(regex, '')
  var match = clean.match(/^[a-z]\w{12}/i)
  console.log(match)
})
ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • Ah, sorry, I'm going to have to edit the question to explain why this probably won't work. – Ryan Peschel Feb 09 '18 at 16:34
  • close, but misses the "exactly 12 chars" constraint – arhak Feb 09 '18 at 16:40
  • @arhak fixed it. – ctwheels Feb 09 '18 at 16:46
  • @RyanPeschel how about now? I dropped `$` from your original regex since it will never match `AgTy5hGvxWa2A` unless it's dropped. – ctwheels Feb 09 '18 at 16:47
  • second regex just for length? why not directly check the length? plain and simple, also more readable – arhak Feb 09 '18 at 16:51
  • I suppose I might end up having to go with something like this if what I'm asking for is unfeasible, but having the input regex match existing password requirement regexes is what I'm trying to go for at the moment. – Ryan Peschel Feb 09 '18 at 16:52
  • @arhak it can't be done in one single regex in JavaScript otherwise. OP specified they want to use their original regex. – ctwheels Feb 09 '18 at 16:52
  • @RyanPeschel wait hold on, what?! You're trying to validate a password? – ctwheels Feb 09 '18 at 16:55
  • I'm not against the two steps, just telling that no need for second check to be a regex, it could simply be `result.length == 12 ? result : false` (don't remember your exact previous code) – arhak Feb 09 '18 at 16:56
  • @arhak user is trying to extract up to 12th char. Not just validate the string length – ctwheels Feb 09 '18 at 16:57
  • if you said so ... From OP I understood not less and not more than 12 `{12}$` – arhak Feb 09 '18 at 17:00
  • That's what I understand too, but original string contains more than 12 word characters, so that would make their initial string invalid (would never get the result they're looking for. So I decided to use the orignal regex. Also, it would technically be a length of 13 otherwise – ctwheels Feb 09 '18 at 17:05