473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regular Expression Mystery

I like to code parameters to my Javascript functions within strings
that look like UNIX command parameters. For instance I might say

myfunc("-center -w:100% -nocache");

I've written a function to extract the value of any one flag or
keyword:value pair, but it's not working. To continue with the example,
I call

get_value(values_passed, "-w:80%");

which means that 'values_passed' is the string that was passed to me,
'-w:' is the keyword I'm interested in and '80%' is its default value.
Here is the function I wrote:

function get_value(parms, key_and_val)
{
var colon = key_and_val.indexOf(':');
var key = key_and_val.substr(0, colon);
var val = key_and_val.substr(colon + 1);
var regex = new RegExp(key + ":(.*)[ $]"); // IS THIS CORRECT?
var rslt = regex.exec(parms);

var result;
if (rslt == null)
{
result = val; // Use default value
}
else
{
result = rslt[0];
}
return result;
}

'rslt' is always null. I've even tried using match() instead of exec():

var rslt = parms.match(regexp);

and that results in 'null' too. Can anyone think of anything I can try
to learn out what's wrong here? Does my regexp violate some syntax rule
I'm unaware of, for instance? Thanks very, very much.

Dean Hannotte
http://www.hannotte.net

Mar 9 '06 #1
2 1272
dhannotte wrote:
I like to code parameters to my Javascript functions within strings
that look like UNIX command parameters. For instance I might say

myfunc("-center -w:100% -nocache");

I've written a function to extract the value of any one flag or
keyword:value pair, but it's not working. To continue with the example,
'not working' is a pretty useless description of your problem :-(

You need to specify what you expect to get and what you actually get,
then explain how they are different, even if it seems pretty obvious to
you. It's much more reliable to have readers skip a bit of wordiness
than have to guess at missing explanations.

I call

get_value(values_passed, "-w:80%");

which means that 'values_passed' is the string that was passed to me,
'-w:' is the keyword I'm interested in and '80%' is its default value.
Here is the function I wrote:

function get_value(parms, key_and_val)
{
var colon = key_and_val.indexOf(':');
var key = key_and_val.substr(0, colon);
var val = key_and_val.substr(colon + 1);
Here's a couple of suggestions that make no difference to the output, so
whether you use them or not is up to you:

var kv_bits = key_and_val.split(':');
var key = kv_bits[0];
var val = kv_bits[1] || '' ;
var regex = new RegExp(key + ":(.*)[ $]"); // IS THIS CORRECT?
It depends on what you are after, see below.

var rslt = regex.exec(parms);

var result;
if (rslt == null) ----------^^^^^^^^^^^^
Note A.

Be careful. If '-w:' is passed as a parameter, then rslt is not null
and rslt[1] will be an empty string ''. Consider:

if (rslt == null || !rslt[1] || rslt[1] == '' )

{
result = val; // Use default value
}
else
{
result = rslt[0]; -----------------------^^^
Note B.

}
return result;
}

'rslt' is always null.
Running get_value() as posted and calling it with:

alert( get_value("-center -w:100% -nocache", "-w:80%") );
In both Firefox and IE '-w:100%' is displayed. Is that 'working' or
not? If -w:100% is changed to -w, then '80%' is displayed. Does that
mean the first result should be '100%'? If so, then change B above to:

result = rslt[1];

'rslt' is always null.


Not for me. Why not have the parameter string entered with spaces
between flags and values, then you can just use split and parse the
resulting array.

e.g.

parseParms("-center -w 100% -nocache")

function parseParms(parms)
{
parms = parms.split(/\s+/);
var parm, val, i=parms.length;

while ( (parm = parms.shift()) ){
if ( --i && parms[0].substring(0,1) != '-' ){
val = parms.shift();
--i;
} else {
val = '';
}

// Do something with parameter & value
alert('Parm: ' + parm + '\nValue: ' + val);
}
}
--
Rob
Mar 10 '06 #2
JRS: In article <11**********************@p10g2000cwp.googlegroups .com>
, dated Thu, 9 Mar 2006 15:26:19 remote, seen in
news:comp.lang.javascript, dhannotte <dh****@nyc.rr.com> posted :
I like to code parameters to my Javascript functions within strings
that look like UNIX command parameters. For instance I might say

myfunc("-center -w:100% -nocache");


That's weird.

IMHO you'd do better to pass an Object (literal or variable)

myfunc({center:true, w:"100%", cache:false})

You don't have to provide all possible values :

function myfunc(A) { var Ht = A.h | 0, ... ; ... }

You'd be able to use 1 & 0 as synonyms for true and false

Javascript should be written using javascript idioms, not those of a
UNIX command shell.

Strings should be used only for material which is intrinsically textual
(except sometimes).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Mar 11 '06 #3

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

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.