472,782 Members | 1,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 3496
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...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.