when using regex object, does one specify "^" as a modifiier?

J

Jake Barnes

This function has always worked for me just fine:

function nl2br_js(myString){
// 02-18-06 - this function imitates the PHP command nl2br, which
finds newlines in a string
// and replaces them with newlines plus HTML BR tags. Is the easiest
way to create the
// appearance of paragraphs when people are creating web pages by
typing text into a form.
var regXString = "\\n"
var regX = new RegExp(regXString, "g");
var replaceString = "<br> \n";
return myString.replace(regX, replaceString);
}



I know that when I create a new Regex object and want to include a
modifier like "g", so the search is global, then I pass that in as a
parameter.

But do I do when I want to include "^" because I only want to match
against the beginning of the line? Do I pass that as a parameter?
 
T

Thomas 'PointedEars' Lahn

Jake said:
This function has always worked for me just fine:

function nl2br_js(myString){
// 02-18-06 - this function imitates the PHP command nl2br, which
finds newlines in a string
// and replaces them with newlines plus HTML BR tags. Is the easiest
way to create the
// appearance of paragraphs when people are creating web pages by
typing text into a form.
var regXString = "\\n"
var regX = new RegExp(regXString, "g");
var replaceString = "<br> \n";
return myString.replace(regX, replaceString);
}

Unnessarily complicated, and insufficient. For almost all practical
purposes nowadays, the following suffices:

function nl2br_js(myString)
{
[...]
But do I do when I want to include "^" because I only want to match
against the beginning of the line? Do I pass that as a parameter?

No. If you did, you would include a literal "^" in the string _to be
replaced_. You have to rewrite that code or write new code, depending
on what you actually intend to do, which you did not make clear at all.


PointedEars
 
R

RobG

Jake Barnes said on 07/04/2006 8:01 AM AEST:
[...]
I know that when I create a new Regex object and want to include a
modifier like "g", so the search is global, then I pass that in as a
parameter.

But do I do when I want to include "^" because I only want to match
against the beginning of the line? Do I pass that as a parameter?

I posted this link to a regular expression tutorial yesterday, it may help:

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regular_Expressions>
 
J

Jake Barnes

Thomas said:
Unnessarily complicated, and insufficient. For almost all practical
purposes nowadays, the following suffices:

function nl2br_js(myString)
{
[...]
But do I do when I want to include "^" because I only want to match
against the beginning of the line? Do I pass that as a parameter?

No. If you did, you would include a literal "^" in the string _to be
replaced_. You have to rewrite that code or write new code, depending
on what you actually intend to do, which you did not make clear at all.

I'm sorry my question was so inexact. I'm wondering how to make a
regex that matches against the begining of a line. Should I do this:

textTypedSoFar = "^" + textTypedSoFar;
var textTypedRegX = new Regex(textTypedSoFar, "i");

or should I do this:

var textTypedRegX = new Regex(textTypedSoFar, "i^");


any help would be appreciated.







function autoSuggestTags() {
// 04-06-06 -

if (document.getElementById("category")) {
var refToCategoryInput = document.getElementById("category")
}

if (refToCategoryInput != undefined) {
var textTypedSoFar = refToCategoryInput.value;
textTypedSoFar = "^" + textTypedSoFar;
var textTypedRegX = new Regex(textTypedSoFar, "i");

var allTagsString =
document.getElementById("output-div").innerHTML;
var allTagsArray = allTagsString.split(",");
var howManyTagsToBeChecked = allTagsArray.length;

if (howManyTagsToBeChecked > 0) {
for (i=0; i < howManyTagsToBeChecked; i++) {
var thisTag = allTagsArray;
if (thisTag.match(textTypedRegX)) {
// more code goes here
}
}
} else {
alert("Error: For some reason, the script failed to get an array
of the most popular tags from Accumulist.");
}
}
}
 
R

RobG

Jake Barnes said on 07/04/2006 10:37 AM AEST:
Thomas said:
Unnessarily complicated, and insufficient. For almost all practical
purposes nowadays, the following suffices:

function nl2br_js(myString)
{
return String(myString || "").replace(/\r?\n|\r/g, "<br>\n");
}

[...]
But do I do when I want to include "^" because I only want to match
against the beginning of the line? Do I pass that as a parameter?

No. If you did, you would include a literal "^" in the string _to be
replaced_. You have to rewrite that code or write new code, depending
on what you actually intend to do, which you did not make clear at all.


I'm sorry my question was so inexact. I'm wondering how to make a
regex that matches against the begining of a line. Should I do this:

textTypedSoFar = "^" + textTypedSoFar;
var textTypedRegX = new Regex(textTypedSoFar, "i");
---------------------------------^^

var textTypedRegX = new RegExp(textTypedSoFar, "i");

or more typically:

var textTypedRegX = new RegExp('^' + textTypedSoFar, "i");
or should I do this:

var textTypedRegX = new Regex(textTypedSoFar, "i^");

'^' is not a flag. Only flags are allowed as the second parameter, using
characters other than g, i or m, or duplicate characters, as the second
parameter will cause an exception.


The first argument to RegExp() is a string, essentially what you would
have put between /.../ in a RegExp literal. One (often confusing)
difference is that quoted special characters, e.g. \d, need to have the
backslash quoted. e.g. to test if a string starts with a digit:

/^\d/.test(...);


is the same as:

var re = new RegExp('^\\d');
re.test(...);


is the same as:

var s = '\\d';
var re = new RegExp('^' + s);
re.test(...);


new RegExp is normally only used where the pattern is unknown until run
time.
any help would be appreciated.
function autoSuggestTags() {
// 04-06-06 -

if (document.getElementById("category")) {
var refToCategoryInput = document.getElementById("category")
}

if (refToCategoryInput != undefined) {

or:

var refToCategoryInput;
if ( document.getElementById
&& refToCategoryInput = document.getElementById("category") ){


Normally the forms & elements collections would be used as they are
usually much more efficient.

var textTypedSoFar = refToCategoryInput.value;
textTypedSoFar = "^" + textTypedSoFar;
var textTypedRegX = new Regex(textTypedSoFar, "i");

// Wrapped for posting, replace all 3 lines with:
var textTypedRegX =
new RegExp('^' + refToCategoryInput.value, 'i');

var allTagsString =
document.getElementById("output-div").innerHTML;
var allTagsArray = allTagsString.split(",");
var howManyTagsToBeChecked = allTagsArray.length;

if (howManyTagsToBeChecked > 0) {

Rather than use an else, do the test up front and return if
howManyTagsToBeChecked is zero:

if (!howManyTagsToBeChecked) {
alert('Error...');
return;
}
for (i=0; i < howManyTagsToBeChecked; i++) {

Keep 'i' local

for (var i=0; i < howManyTagsToBeChecked; i++) {

If the order of checking is not important, a while loop may be simpler:

var i = allTagsArray.length;
while (i--){
var thisTag = allTagsArray;
if (thisTag.match(textTypedRegX)) {
// more code goes here
}
}

[...]

function autoSuggestTags()
{
var refToCategoryInput,
textTypedRegX,
allTagsString,
allTagsArray,
howManyTagsToBeChecked,
thisTag;

if ( document.getElementById
&& (refToCategoryInput = document.getElementById("category"))){

textTypedRegX = new RegExp('^' + refToCategoryInput.value, 'i');
allTagsString = document.getElementById("output-div").innerHTML;
allTagsArray = allTagsString.split(",");
howManyTagsToBeChecked = allTagsArray.length;

if (!howManyTagsToBeChecked){
// Handle error
return;
}

while (howManyTagsToBeChecked--){
thisTag = allTagsArray[howManyTagsToBeChecked];

if (thisTag.match(textTypedRegX)) {
// more code goes here
}
}
}
}
 
J

Jake Barnes

RobG said:
new RegExp is normally only used where the pattern is unknown until run
time.

Thanks so much for all the help. And yes, the pattern is based on what
the user is typing, so the pattern is unknown till run time.



or:

var refToCategoryInput;
if ( document.getElementById
&& refToCategoryInput = document.getElementById("category") ){


Normally the forms & elements collections would be used as they are
usually much more efficient.

Thanks for reminding me to check for document.getElementById. My code
is terribly sloppy. I'm not sure what you mean about the forms and
elements collections, why are they better than document.getElementById?


Rather than use an else, do the test up front and return if
howManyTagsToBeChecked is zero:

if (!howManyTagsToBeChecked) {
alert('Error...');
return;
}

This is standard?


If the order of checking is not important, a while loop may be simpler:

var i = allTagsArray.length;
while (i--){
var thisTag = allTagsArray;
if (thisTag.match(textTypedRegX)) {
// more code goes here
}
}

[...]


The order is important. The words being checked are in order of
popularity.


Thanks much for all the help.
 
R

RobG

Jake said:
Thanks so much for all the help. And yes, the pattern is based on what
the user is typing, so the pattern is unknown till run time.

That should have been (note extra brackets):

&& (refToCategoryInput = document.getElementById("category"))){

Thanks for reminding me to check for document.getElementById. My code
is terribly sloppy. I'm not sure what you mean about the forms and
elements collections, why are they better than document.getElementById?

Using statements like:

var someElement = document.forms['form_name'].elements['element_name'];


and variants.

It is more widely supported than getElementById (though only marginally
since 'version 4' browsers are nearly extinct). It is more efficient
because once you have a reference to the form you can access the
elements easily, e.g.:

var formRef = document.form['from_name'];
var someEl = formRef.element_name;

or

var someEl = formRef.elements['element_name'];


rather than lots of calls to getElementById.
This is standard?

Yes. If the value of howManyTagsToBeChecked is zero, it will evaluate
to false. ! negates it to true so the alert is shown and the function
returns. It is equivalent to:

if (howManyTagsToBeChecked == 0) {
//...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top