ldsmike88 Posted March 9, 2007 Share Posted March 9, 2007 I have a line of code that looks something like this: var myTextareaValue = textareaValue.replace('\n','<br/ >'); That works fine when I only have one line break at a time. But if I have multiple line breaks it only puts one <br/ > tag in instead of two or three. How can I make it so it will put in the right amount of break tags. Thanks! Michael (I know my break tags in this forum aren't formatted correctly, but if they are they actuly are break tags and you can't see them.) Quote Link to comment Share on other sites More sharing options...
warewolfe Posted March 10, 2007 Share Posted March 10, 2007 change var myTextareaValue = textareaValue.replace('\n','<br/ >'); to var myTextareaValue = myTextareaValue.replace(/\n/g, /<b r>/ ); The g switch makes the changes global. I'm not certain if you can escape the forward slash in the <b r /> replacement section but if you can it'll probably look like /<br \/>/ I added a space in the br to stop the line from breaking Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 10, 2007 Author Share Posted March 10, 2007 so is the first term in the replace function 'forwardslash backslash n forwardslash g'? Or did you add one of those slashes to prevent it from executing something? Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 10, 2007 Author Share Posted March 10, 2007 That didn't do anything different. With '\n' and with '/\n/g' it only inserted one break. For example. If I put in here is some text it puts out here is some text Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 12, 2007 Author Share Posted March 12, 2007 Well the problem was that it only fixed the first line break. I got it to work now. Thanks! This is the code I had to use: while(this.value.match('<br />') == '<br />'){ this.value = this.value.replace('<br />','\n'); } Quote Link to comment Share on other sites More sharing options...
fenway Posted March 13, 2007 Share Posted March 13, 2007 You're simply missing the /g modifier -- you don't need to do a while you. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 13, 2007 Author Share Posted March 13, 2007 I don't think I even know what the /g modifier is or what it does. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 14, 2007 Share Posted March 14, 2007 It replaces all matches of your regex... it's a global modifier. this.value = this.value.replace(/<br \/>/g,'\n'); Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 15, 2007 Author Share Posted March 15, 2007 So the initial slash is like the first quote and the /g is like the last quote? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 15, 2007 Share Posted March 15, 2007 You should read up on JS regexes, but yes. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.