470,585 Members | 1,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,585 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

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

where x is any character

Jul 23 '05 #1
1 3379
"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.
<textarea rows="5" cols="60">
Here's a matched sequence : 12345,123
<input type="button"
onclick="match('\\d{5},\\d{3}', this.form.elements[0])">

<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);
((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).
rng.moveStart("character", start-corr);
rng.moveEnd("character", length);
return true;

function dummy(){
return false;

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

return match(pattern, textarea);
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
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.