468,104 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,104 developers. It's quick & easy.

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

Jul 23 '05 #1
1 3246
Yep
"Volt" wrote :
i need to select the first string in textarea whitch is like xxxxx,xxx


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.elements[0])">
</form>

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

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

function moz_match(textarea, start, length){
var v=textarea.value;
textarea.setSelectionRange(start, start+length);
textarea.scrollTop=textarea.scrollHeight*
((v.substr(0, start).replace(/[^\n]/g, "").length-1)/
((v.replace(/[^\n]/g, "")).length))|0;
return true;
}

function ie_match(textarea, start, length){
var rng, corr=textarea.value.substr(0, start).
replace(/[^\r]|\r[^\n]/g,"").length;
rng=textarea.createTextRange();
rng.moveStart("character", start-corr);
rng.collapse(true);
rng.moveEnd("character", length);
rng.select();
return true;
}

function dummy(){
return false;
}

match=textarea.setSelectionRange && makeMatcher(moz_match) ||
textarea.createTextRange && makeMatcher(ie_match) ||
dummy;

return match(pattern, textarea);
}
</script>
---
HTH
Yep.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Gaze | last post: by
7 posts views Thread by Voetleuce en fÍnsievry | last post: by
5 posts views Thread by Andrew V. Romero | last post: by
3 posts views Thread by Sunil Kulkarni | last post: by
19 posts views Thread by dmiller23462 | last post: by
5 posts views Thread by Michael Hill | last post: by
1 post views Thread by JosAH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.