text select in textarea (regular expression)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Volt

    text select in textarea (regular expression)

    is there any way to select and mark part of text in textarea by regular
    expression?

    i need to select the first string in textarea whitch is like xxxxx,xxx

    where x is any character

  • Yep

    #2
    Re: text select in textarea (regular expression)

    "Volt" wrote :
    [color=blue]
    > i need to select the first string in textarea whitch is like xxxxx,xxx[/color]

    You can use ranges to retrieve and highlight text, but few browsers
    support them, namely IE4+ and recent Gecko-based browsers; the
    following should work on IE5.5+ and Mozilla; if you need to support
    older IE then you'll have to emulate $x properties on RegExp.


    ---
    <form>
    <textarea rows="5" cols="60">
    Here's a matched sequence : 12345,123
    </textarea>
    <input type="button"
    value="match()"
    onclick="match( '\\d{5},\\d{3}' , this.form.eleme nts[0])">
    </form>

    <script type="text/javascript">
    function match(pattern, textarea){

    function makeMatcher(fun c){
    return function(re, ta) {
    return !!
    new RegExp(re).exec (ta.value) &&
    func(ta, RegExp["$`"].length, RegExp["$&"].length);
    }
    }

    function moz_match(texta rea, start, length){
    var v=textarea.valu e;
    textarea.setSel ectionRange(sta rt, start+length);
    textarea.scroll Top=textarea.sc rollHeight*
    ((v.substr(0, start).replace(/[^\n]/g, "").length-1)/
    ((v.replace(/[^\n]/g, "")).length))|0 ;
    return true;
    }

    function ie_match(textar ea, start, length){
    var rng, corr=textarea.v alue.substr(0, start).
    replace(/[^\r]|\r[^\n]/g,"").length;
    rng=textarea.cr eateTextRange() ;
    rng.moveStart(" character", start-corr);
    rng.collapse(tr ue);
    rng.moveEnd("ch aracter", length);
    rng.select();
    return true;
    }

    function dummy(){
    return false;
    }

    match=textarea. setSelectionRan ge && makeMatcher(moz _match) ||
    textarea.create TextRange && makeMatcher(ie_ match) ||
    dummy;

    return match(pattern, textarea);
    }
    </script>
    ---


    HTH
    Yep.

    Comment

    Working...