473,416 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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 3537
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gaze | last post by:
Hi, I use <textarea> input more than one paragraph to the database, whose field type is set to "text". Then I retrieve it to display in <textarea>, which is OK, paragraph by paragraph, but not...
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
5
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript...
3
by: Sunil Kulkarni | last post by:
I need to validate my TextArea as follows: 1) IT SHOULD ALLOW FOR ONLY 5 COLUMNS 2) IT SHOULD ALLOW ONLY 65 CHARACTERS PER ROW I need some kind of a JS validation for the same. I have tried...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
1
by: JosAH | last post by:
Greetings, Introduction This week we start building Query objects. A query can retrieve portions of text from a Library. I don't want users to build queries by themselves, because users make...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.