Lasso Soft Inc. > Home

  • Articles

Handy regexp to find all form field names in HTML with Lasso 9

By Jolle Carlestam

I was asked for a regexp solution that would find all form field names in an HTML file and return only a list of the names with everything else removed.

Since I misunderstood the demand I first created a Lasso 9 solution. Turned out the need was for a Bbedit version. So, anyway, here's what I came up with:

Lasso 9:

var(myhtml = '
<form action="./" method="post">
	<input type="hidden" name="-action" value="myaction" />
	<div>
		<p>
			<label for="fname" id="fname_label">
				First Name
			</label>
			<input type="text" name="fname" class="" id="fname" value="" size="30" />
		</p>
	</div>
	<div>
		<p>
			<label for="mname2" id="mname2_label">
				Middle Name
			</label>
			<input type="text" name="mname" class="" id="mname2" value="" size="30" />
		</p>
	</div>
	<div>
		<p>
			<label for="record_title" id="record_title_label">
				Last Name *
			</label>
			<input type="text" name="lname" class="" id="record_title" value="" size="30" />
		</p>
	</div>
	<div>
		<p>
			<label for="status12" id="status12_label">
				Status *
			</label>
			<select name="status" class="" id="status12">
				<option value="Active" selected="selected">
					Active
				</option>
				<option value="Inactive">
					Inactive
				</option>
				<option value="Blocked">
					Blocked
				</option>
			</select>
		</p>
	</div>
	<input type="hidden" value="true" name="retries" />
	<input type="submit" name="button_add" class="button first" id="button_add21" value="Add" />
	<input type="submit" name="button_cancel" class="button cancel" id="button_cancel22" value="Cancel" />
</form>
'
)

var(myregexp = regexp(-find = ` name *= *("|')(.*?)("|')`, -input = $myhtml, -ignorecase))

var(myresult = array)
while($myregexp -> find) => {
	$myresult -> insert($myregexp -> matchstring(2))
}
'<pre>'
$myresult -> join('\r')
'</pre>'

BBEdit:

Search for: ^(.*? name *= *)?(?(1)("|')(.*?)("|').*?(\r)|.*?\r)
Replace with. \3\5

Result in both:

-action
fname
mname
lname
status
retries
button_add
button_cancel

 

Note that the Bbedit version will fail if there's more than one name value on a row. The Lasso 9 solution handles that fine.

Author: Jolle Carlestam
Created: 7 May 2012
Last Modified: 7 May 2012

Comments

No comments found
You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft