在Javascript中搞砸了正则表达式

时间:2014-01-08 23:43:53

标签: javascript jquery regex

我正试图解决这个问题:

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

来自:

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

我的正则表达式就是这样:

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

给了我这个:

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

那么,我需要在我的正则表达式中进行哪些更改以允许我获取我想要的字符串?来自所说的字符串?

3 个答案:

答案 0 :(得分:4)

试试这个:

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

THE PONY HE COMES你绝对没有任何借口,因为你在使用JavaScript!

答案 1 :(得分:1)

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

答案 2 :(得分:0)

尝试

qJumpHTML.replace(/.*(<select.*?<\/select>).*/gi,'$1');