SitePoint Sponsor |
|
User Tag List
Results 1 to 17 of 17
Thread: need a little RegEx help
-
Feb 11, 2007, 23:08 #1
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
need a little RegEx help
I have some very basic code that takes a newline and changes it to a <br/>
Code:post=post.replace(/[^>]\n/g, "<br/>\n");
test=
<b>Hello</b>
test
test<br/>
<b>Hello</b>
tes<br/>
how do I keep it from deleting the last character?
-
Feb 11, 2007, 23:30 #2
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
the problem is it deletes the last character, so test turns into tes<br/>
test=\n
test<br/>
then this will happen:
test\n
tes<br/>
You can't have the replacement for the '=' in the first one and then not have the replacement for the 't' in the second one(unless you make your regex less general). You could do this:
post=post.replace(/([^>])\n/g, "$1<br/>\n");
$1 grabs the actual match to the first parenthesized grouping in the regex ($2 does the same for the second parenthesized grouping, etc.).
You should also know that a newline is different on different operating systems:
windows: \r\n
mac: \r
unix: \nLast edited by 7stud; Feb 13, 2007 at 16:14.
-
Feb 12, 2007, 08:17 #3
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I was trying $1 before but it didn't work because I didn't have the parenthesis
thanks for your help, it works as intended
-
Feb 12, 2007, 22:54 #4
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
okay, small problem - it doesn't work for multiple newlines
EXAMPLE:
test+
test
test
WILL BE:
test+<br/>
test<br/>
test
any suggestions? I tried a few things but none worked
-
Feb 13, 2007, 01:04 #5
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I tried a few things but none worked
\n+
Is the text always going to be coming from someone using a UNIX operating system? If not, reread the end of post #2.
-
Feb 13, 2007, 11:23 #6
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That doesn't work.
It takes this:
test.
<b>test</b>
test
test.<br/>
<b>test</b>
test<br/>
test.<br/>
<br/>
<b>test</b>
test<br/>
I want each newline to have a <br/>
I understand why it's not working, but I can't find a solution.
This will only be used on a Windows workstation.
-
Feb 13, 2007, 16:16 #7
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This will only be used on a Windows workstation.
-
Feb 13, 2007, 16:19 #8
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 13, 2007, 17:19 #9
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
And what is the newline character on a Windows operating system?It's still \n
so the code works
-
Feb 14, 2007, 00:12 #10
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
For the second parameter of replace(), instead of specifying a string, e.g.
var result = str.replace(/[a-z]/, "x");
you can specify a function which returns a string, e.g.:
var result = str.replace(/[a-z]/, someFunc);
js calls the function you specify and automatically sends it certain arguments; then the function needs to return a string that js will use as the replacement string. The first argument js sends the function is the actual match to the full regex. The second argument is the actual match to the first parenthesized grouping. The third argument is the actual match to the second parenthesized grouping. And so on until all the parenthesized groupings are exhausted. Finally, the pos of the full match in the source string is returned as the 2nd to last argument, and the last argument is the source string.
Try this:
Code:var str = "test.\r\n\r\n\r\n<b>test</b>\r\ntest"; var regex = /([^>])((\r\n)+)/; var newStr = str.replace(regex, myFunc) function myFunc(fullMatch, subMatch1, subMatch2) { var str = subMatch1; var breakCount = subMatch2.length/2; for(var i=0; i<breakCount; ++i) { str+="<br/>\n"; } return str; } alert(str + "\n-----\n" + newStr);
Last edited by 7stud; Feb 14, 2007 at 01:19.
-
Feb 14, 2007, 06:07 #11
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just remove the [^>] part
Code:str = str.replace(/\n/g, "\n<br/>");
-
Feb 14, 2007, 15:45 #12
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just remove the [^>] part
-
Feb 14, 2007, 17:31 #13
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
7stud the whole point of this function is take text from a <textarea> and add <br/> tags wherever there's a new line
the whole point of [^>] is because I don't want a newline after block elements (div, li, etc.)
That said, I can't understand the difference between \n and \r and how it applies to this situation.
I can probably look at the source code of any forum textarea and just copy the code, but I wanted to learn it this way so I can understand what's happening.
-
Feb 15, 2007, 05:40 #14
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:post=post.replace(/([^>])\n/g, "$1<br/>\n");
Code:// convert different newline forms to a single LF s = s.replace(/\r\n?/g, "\n") // remove LF after > s = s.replace(/>\n/g, ">") // replace LF into <br> s = s.replace(/\n/g, "<br>\n")
-
Feb 16, 2007, 10:34 #15
- Join Date
- Jul 2004
- Location
- Brooklyn, NY
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks stereofrog!
that was exactly what I needed
Is it possible to do a "negative lookbehind" with PHP? What would it look like?
-
Feb 16, 2007, 12:08 #16
-
Feb 16, 2007, 13:34 #17
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks