Hi all

here is my problem,
I have a text area in html page containing a long html code.
like

some code
.....

.......

<input type="text" name="mobile" value="1234567890"/>


some code
.....

.......some code
.....

.......

From that code present in the <textarea> i need to extract following line

<input type="text" name="mobile" value="1234567890"/>


I want to write a java script Function which will search the entire code find the pattern <input type="text" name="mobile" and extract the value 12345690 using RegEx.

Recommended Answers

All 10 Replies

Member Avatar for stbuchok

Just out of curiosity, why the need for regex? Why not use document.getElementById() and give the textarea an id attribute:

<input type="text" id="mobile" value="1234567890" />

<script>

alert(document.getElementById('mobile').value);

</script>

You could also use document.getElementsByName (this will return an array of elements), but I've always preferred id (getElementById will return the first element with that id).

Well thanks for reply but the problem is the text is not the element of the page!So in my case i cant access that item like this.how can i find that using regular expression ??

Member Avatar for stbuchok

I don't understand, if it's not on the page, how are you supposed to parse it using javascript?

Can you give an explanation as to everything that is going on? There might be an easier way to do this, if I can understand the situation.

Actually I am pasting the whole code into that text area.
I am trying to make script if some one click a button it will search the text field and extract the phone number from that code pasted in textarea.

<textarea rows="31" cols="40" NAME="firstName" onchange="showAndClearField(this.form)">
....

....

code code code \

<input type="text" name="mobile" value="1234567890"/>

more code more code

.....
....

......

</textarea>

please let me know if m not clear.

Something like this probably.

result = subject.match(/<input type="text" name="mobile" value="(.*?)"\/>/g);
commented: Great Sol. +3
Member Avatar for stbuchok

This is a way of doing without Regex:

<html>
<head>
<title>this is first math exercise within Java Script</title>

<script lanuage="Javascript">

function extractById(fieldId){
	var div = document.getElementById('placeholder');

	div.innerHTML = document.getElementById('txt').value;

	var input = document.getElementById(fieldId);

	alert(input.value);
}

function extractByName(fieldName){
	var div = document.getElementById('placeholder');

	div.innerHTML = document.getElementById('txt').value;

	var input = document.getElementsByName(fieldName);

	for(var i = 0; i < input.length; i++){
		alert(input[i].value);
	}
}

</script>

</head>

<body>

<div id="placeholder" style="display: none;"></div>

<textarea id="txt" style="width: 500px; height: 300px;"></textarea>
<button onclick="extractById('mobile1'); return false;">Extract By ID</button>

<button onclick="extractByName('mobile2'); return false;">Extract By Name</button>

</body>

</html>

You can use the following to test with:

<input type="text" id="mobile1" value="1111111111"/>
<input type="text" name="mobile2" value="2222222222"/>
<input type="text" name="mobile2" value="3333333333"/>

Using regex can get tricky for parsing this type of information. If you always know the name or id this will be more accurate.

Member Avatar for stbuchok

or what pritaeas said (I still like my solution ;) )

Well Thanks a lot pritaeas its working fine but its returning the whole line :

<input type="text" name="mobile" value="1234567890"/>


I just want to get the Number in this case it should return 1234567890!!

Something like this probably.

result = subject.match(/<input type="text" name="mobile" value="(.*?)"\/>/g);

Well Thanks a lot pritaeas its working fine but its returning the whole line :

<input type="text" name="mobile" value="1234567890"/>


I just want to get the Number in this case it should return 1234567890!!

Hmz... it should return an array.

See this page, first example (exec). Perhaps that'll work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.