50
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

How can I remove everything between H and S so that the outcome would be ABCDEFGHSTUVWXYZ?

UserIsCorrupt
  • 4,837
  • 15
  • 38
  • 41
  • 1
    Just use `ABCDEFGHSTUVWXYZ`. Read between the words: rephrase your question. Do you want every letter between H and S to be removed? Are the first and last letter variable? – MaxArt May 21 '12 at 17:33
  • 8
    I can't just use `ABCDEFGHSTUVWXYZ`, this was only an example. Obviously I know removing it manually is an option. – UserIsCorrupt May 21 '12 at 17:38
  • 1
    Yes, but if you provide a less-simple example (unless your use-case really *is* that simple) it might help us to provide you with a better/more-applicable solution to your problem. – David Thomas May 21 '12 at 17:40

2 Answers2

80

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(  alphabet.replace(/H.*S/, 'HS')  )

Or just:

var alphabet = "ABCDEFGHSTUVWXYZ";
vsync
  • 118,978
  • 58
  • 307
  • 400
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Hello, I have HTML source from which i need to remove all script tags. I have tried below code but it also removes code between 2 instance. Code: ``'Hello this is that you '.replace(/ – Nilay Mehta May 11 '18 at 00:57
  • 1
    @Nilay You should ask new questions instead of commenting on other ones. I'll try to answer here though. You really should never do stripping of `ipt>alert('bad');` in it? What about: ``? – Paul May 11 '18 at 17:20
  • 2
    @Nilay Instead you should whitelist specific tags and attributes that are allowed, or better use **BBCode**, or even better **Markdown**. I'm sure that in whatever language you're using, you can find some library for compiling Markdown to HTML. – Paul May 11 '18 at 17:22
  • @Nilay As an aside, to get the output you wanted you should make your regex `.*` non-greedy by changing it to `.*?` and make your regex global with the `g` flag: `/ – Paul May 11 '18 at 17:28
  • Thanks @paulpro, I was tried ``/ – Nilay Mehta May 14 '18 at 06:49
  • On the other hand, if you have multiple occurrences of H and S in the String, it won't work as necessarily expected. It will take the first H and the last S, and replace everything in between (including other H's and S's) with HS. If you want to avoid this behavior, try this regex that I just made on [regex101](https://regex101.com) which seems to be working: `H([^HS]{1,})S`. It essentially replaces any sequence of chars that are not H or S, between an H and an S. – Cannicide Mar 25 '20 at 19:03
11
var strippedAlphabet = alphabet.replace(/H.*S/, 'HS');
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 2
    `"___H___S_____H__S____".replace(/H.*S/, 'HS')` isn't as the expected output: `___HS____HS____` – vsync Aug 22 '19 at 18:44
  • @vsync I was just experimenting on regex101 and figured out this regex which seems to work properly and produce the expected output: `H([^HS]{1,})S`. I tested it with the case in your comment and it produced the expected output. Just leaving it here in case anyone else needs it. Obviously I can't post it as an answer since this question has been closed, though :( – Cannicide Mar 25 '20 at 19:02