0

I'm trying to get this:

<select name="id" onchange="window.location=('viewforum.php?id='+this.options[this.selectedIndex].value)">
    <optgroup label="Test category">
        <option value="2">New forum</option>
        <option value="3" selected="selected">New forum</option>
        <option value="4">New forum</option>
        <option value="5">New forum</option>
        <option value="1">Test forum</option>
    </optgroup>
</select>

from this:

<div>
    <label>
        <span>Jump to<br></span>
        <select name="id" onchange="window.location=('viewforum.php?id='+this.options[this.selectedIndex].value)">
            <optgroup label="Test category">
                <option value="2">New forum</option>
                <option value="3" selected="selected">New forum</option>
                <option value="4">New forum</option>
                <option value="5">New forum</option>
                <option value="1">Test forum</option>
            </optgroup>
        </select>
        <input type="submit" value=" Go " accesskey="g">
    </label>
</div>

And my Regex which is this:

qJumpHTML.replace(/.*\<select|\<\/select*/gi,''); 

is giving me this:

<div>
    <label>
        <span>Jump to<br></span>
            name="id" onchange="window.location=('viewforum.php?id='+this.options[this.selectedIndex].value)">
        <optgroup label="Test category">
            <option value="2">New forum</option>
            <option value="3" selected="selected">New forum</option>
            <option value="4">New forum</option>
            <option value="5">New forum</option>
            <option value="1">Test forum</option>
        </optgroup>
        >
        <input type="submit" value=" Go " accesskey="g">
    </label>
</div>

So, what do I need to change in my Regex to allow me to get the string I want? from said string?

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
CyanPrime
  • 5,096
  • 12
  • 58
  • 79
  • Can you simplify the example? It is hard to understand what is your problem with so much code... – nsx Jan 08 '14 at 23:46
  • There's only one line of actual code. All that HTML is just a string. The line to look at is the Regex. I'm basically trying to get all of the string between the – CyanPrime Jan 08 '14 at 23:49
  • Simplifying the example is still good advice, for your programming in general, when things aren't working out at first – Dexygen Jan 08 '14 at 23:53
  • Would it make more sense to manifest the string into DOM and retrieve the – Swivel Jan 08 '14 at 23:53

3 Answers3

4

Try this:

var d = document.createElement('div');
d.innerHTML = qJumpHTML;
var result = d.getElementsByTagName('select')[0].outerHTML;

THE PONY HE COMES and you have absolutely NO excuse since you're working in JavaScript!

Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

/(<select[^>]*>.*?</select>)/gs

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
0

Try

qJumpHTML.replace(/.*(<select.*?<\/select>).*/gi,'$1'); 
Palpatim
  • 9,074
  • 35
  • 43
  • Thank you for trying, but that just gave me the full string. – CyanPrime Jan 08 '14 at 23:51
  • This works if you use `'$1'` instead of `'\1'`. I think we're generally encouraged to use `$1` instead of `\1` for replace strings (the regex itself is another matter). Although I'm surprised Palpatim's answer failed in this particular way. – David Knipe Jan 08 '14 at 23:56