471,602 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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


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?

Apr 6 '06 #1
6 1259
Jake Barnes wrote:
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)
{
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.
PointedEars
Apr 6 '06 #2
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>
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 7 '06 #3

Thomas 'PointedEars' Lahn wrote:
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");

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[i];
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.");
}
}
}

Apr 7 '06 #4
Jake Barnes said on 07/04/2006 10:37 AM AEST:
Thomas 'PointedEars' Lahn wrote:
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[i];
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
}
}
}
}


--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 7 '06 #5

RobG wrote:
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[i];
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.

Apr 7 '06 #6
Jake Barnes wrote:
RobG wrote:
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") ){
That should have been (note extra brackets):

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


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.
Rather than use an else, do the test up front and return if
howManyTagsToBeChecked is zero:

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

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) {
//...
}
--
Rob
Apr 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Flos | last post: by
14 posts views Thread by john.burton.email | last post: by
5 posts views Thread by Mark Johnson | last post: by
2 posts views Thread by Jeff Jarrell | last post: by
16 posts views Thread by Mark Chambers | last post: by
13 posts views Thread by marathoner | last post: by
14 posts views Thread by =?GB2312?B?zPC5zw==?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by

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.