473,651 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic "match" / regex?

Hi there

Ideally, I'd like to create one javascript function and pass the file
extension i'm looking for to see if its there:

<input type="file" onchange="check File('pdf',this )" />

and then have the function something like (w/ Prototype)
function checkFile(theEx tension,theFiel d) {
var field = $(theField);
var fieldName = field.name;
var fieldID = field.id;
var fieldValue = field.value;
var matchPattern = "/\."+theExtensio n+"$/im";
if (fieldValue.mat ch(matchPattern )) {
alert('Its a '+theExtension+ '!');
} else {
alert('no!');
}
}
The problem is, the line with "fieldValue.mat ch" is not using the "var
matchPattern" value (my RegEx expression)...

Can anyone make this work / must i use 'eval' to get the match to work
against the dynamic var I created?

Jul 3 '07 #1
3 2473
Good Man <he***@letsgo.c omwrote in news:Xns9962A25 2BF8F8sonicyout h@
216.196.97.131:
Hi there

Ideally, I'd like to create one javascript function and pass the file
extension i'm looking for to see if its there:

<input type="file" onchange="check File('pdf',this )" />

yes, the form field is properly made in real life:

<input type="file" id="myFile" name="myFile" onchange="check File
('pdf',this)" />
Jul 3 '07 #2
On Jul 4, 5:56 am, Good Man <h...@letsgo.co mwrote:
Hi there

Ideally, I'd like to create one javascript function and pass the file
extension i'm looking for to see if its there:

<input type="file" onchange="check File('pdf',this )" />

and then have the function something like (w/ Prototype)

function checkFile(theEx tension,theFiel d) {
var field = $(theField);
var fieldName = field.name;
var fieldID = field.id;
var fieldValue = field.value;
var matchPattern = "/\."+theExtensio n+"$/im";
Try:

var matchPattern = new RegExp('\\.' + theExtension + '$','im');

When constructing a regular expression this way you must quote
backslashes, hence \\, so \b becomes \\b, \n becomes \\n, etc.
if (fieldValue.mat ch(matchPattern )) {
If all you are doing is matching the RegExp, use test:

if (matchPattern.t est(fieldValue) ) {
--
Rob

Jul 3 '07 #3
RobG <rg***@iinet.ne t.auwrote in news:1183495647 .365626.100600
@j4g2000prf.goo glegroups.com:
> var matchPattern = "/\."+theExtensio n+"$/im";

Try:

var matchPattern = new RegExp('\\.' + theExtension + '$','im');

When constructing a regular expression this way you must quote
backslashes, hence \\, so \b becomes \\b, \n becomes \\n, etc.
> if (fieldValue.mat ch(matchPattern )) {

If all you are doing is matching the RegExp, use test:

if (matchPattern.t est(fieldValue) ) {

Many thanks RobG...
Jul 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2068
by: kmunderwood | last post by:
I am trying to combine "if match=" and "when test" I am a newbie, and have made both work separately, but I can not seem to combine them. This is my xml("index.xml")page(I can not change this, it comes to me this way. <?xml version="1.0" encoding="iso-8859-1" ?>
7
1677
by: Chris Mantoulidis | last post by:
I was wondering if there was something like a "match" class in the std. By match class I mean something like a search tool. That is I will tell it in what text to search and WHAT to search for. The format of what I want it to find would be something like: "abc?efg*mnopq?st*z" I hope you understand what I mean. :) So it a class (or anyway SOMETHING) in the std header files that is something like that or do I have to implement it myself?
32
14653
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression engine doesn't exaust all the
1
1237
by: sonia93 | last post by:
Hi , I am having a variable which has "++" or "+" as part of it . My file consists something like this air++ water fire+ I want to go one by one to each line and match a particular word of that line.
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.