473,218 Members | 1,443 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,218 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 1322
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Flos | last post by:
Hello! I want to make a validation for a string which can be only letters AND space. for example "apple house", but not "apple3 house" i have found if (preg_match ("//", $string)) return...
1
by: Naresh Agarwal | last post by:
Hi I'm using SAX Parser of Xerces Java v2.4.0 for XML Parsing. I want to perform schema validations on the xml. The problem is that root element of XML document does not have...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
5
by: Mark Johnson | last post by:
Regex("@("); brings an error (missing ")"). How do you serarch for a ( with Regex ? Mark Johnson, Berlin Germany mj10777@mj10777.de
2
by: Jeff Jarrell | last post by:
I want to use the regex.replace for a string containing "%s" I can't seem to get the "%s" escaped. I tried a normal "\%s" but that doesn't seem to do it. Picks up any "s"....
2
by: Ed Staffin | last post by:
In Internet Explorer under the tools/internet options menu on the general tab, there is a settings button under Temporary internet files. In there the user can specify when to "Check for newer...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
13
by: marathoner | last post by:
I would like to read the following entries of mixed data types from a ascii text file using C#. This can be easily performed in C using fscanf. Is there an equivalent function in C#? 1 2...
14
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I wonder why below does not work. a = object() a.b = 1 # dynamic bind attribute failed... To make it correct, we have to create a new class: class MyClass(object): pass a =...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.