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
}
}
}
}