4

First off I don't know much about regex and need to buy a book because it's has proven to me to be difficult to pickup.

Ultimately I want to take a dom element, and replace text within straight brackets "[" and "]" and insert a link around the text, and there may be multiple bracket sets in the string.

function changeTip() {  
        var link = '<a href="' + $('#txtURL').attr('value') + '" target="_blank">';
        $('.tipoftheweektip').html($('#txtTip').attr("value").replace('[', link).replace(']', '</a>'));
    }

This works except:

  • doesnt work on the second set of brackets
  • if there isnt a closing straight bracket, it deletes all the text before the opening straight bracket

I've looked at examples and because straight brackets are used in the regex code, I cant figure out how to look for a bracket and replace it.

Anyone out there that has done something similar that they can share? Thanks in advance.

Jan Wikholm
  • 1,557
  • 1
  • 10
  • 19
jonathan hall
  • 1,386
  • 3
  • 10
  • 7

2 Answers2

11
.replace(/\[([^\]]*)\]/g, link + "$1</a>")

which means, find text between [ and ] and replace it with the value of link, the text itself and ''. This ensures matching square brackets. The 'g' means 'do it multiple times (globally)'.

Doug Domeny
  • 4,410
  • 2
  • 33
  • 49
  • This site provides a place to test regular expressions in either JavaScript or .NET. It also contains many samples, but they are not always correct. http://www.regexlib.com/RETester.aspx – Doug Domeny Jun 24 '09 at 22:22
  • thanks! You're right, I wasnt thinking about finding matching brackets. I learn something everyday! – jonathan hall Jun 24 '09 at 22:43
  • That g flag isn't portable across browsers. – Paul Biggar Jan 24 '13 at 22:21
  • @PaulBiggar, I've used the global flag for years across multiple browsers. Which browsers/versions don't support the g flag? If the problem is the `/`...`/g` syntax, then does the constructor work? For example, `new RegExp("\\[([^\\]]*)\\]", "g")` – Doug Domeny Jan 25 '13 at 12:54
0

This will loop through an entire string and replace it with your chosen word or phrase with another. (kind of complicated but it works and is very reusable)

var string = "this is some test text. You can replace all instances of any word/phrase within this text"

var new string = string.findAndReplace("text", "BOO!");

Object.prototype.findAndReplace = function( searchText, replace ) {
    var matchCount = 0;
        var text = this;

        for( var i = 0; i<text.length; i++ ) {
            var textSearched = "";

            for(var x = 0; x<searchText.length; x++) {
                var currentText = text[i+x];

                if( currentText != undefined ) {
                    textSearched += currentText;
                }
            }

            if( textSearched == searchText ) {
                matchCount++;
            }

            console.log( textSearched );
        }

        for(var i=0; i<matchCount; i++) {
            text = text.replace( searchText, replace );
        }

        return text;
    }
Danny Blue
  • 463
  • 4
  • 16