0

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.

3
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by pritaeas
Featured Replies
  • 1

    Something like this probably. [CODE=javascript] result = subject.match(/<input type="text" name="mobile" value="(.*?)"\/>/g); [/CODE] Read More

0

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).

0

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 ??

0

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.

0

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.

1

Something like this probably.

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

Edited by pritaeas: n/a

Votes + Comments
Great Sol.
0

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.

0

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!!

0

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!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.