954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Javascript Searching a pattern

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 i need to extract following line


I want to write a java script Function which will search the entire code find the pattern

debasishgang7
Junior Poster in Training
91 posts since Oct 2009
Reputation Points: 10
Solved Threads: 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).

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

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

debasishgang7
Junior Poster in Training
91 posts since Oct 2009
Reputation Points: 10
Solved Threads: 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.

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

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.


....

....

code code code \

more code more code

.....
....

......

please let me know if m not clear.

debasishgang7
Junior Poster in Training
91 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Something like this probably.

result = subject.match(/<input type="text" name="mobile" value="(.*?)"\/>/g);
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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.

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

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

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

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


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

debasishgang7
Junior Poster in Training
91 posts since Oct 2009
Reputation Points: 10
Solved Threads: 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 :


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

debasishgang7
Junior Poster in Training
91 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Hmz... it should return an array.

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: