From: Tom Werner Date: 2006-06-22T03:34:50+09:00 Subject: Re: Why this code doesn't work? James Edward Gray II wrote: > On Jun 21, 2006, at 1:21 PM, Alan Moen wrote: > >> Hi, >> >> I am trying to replace all html tags and spaces with "*" in a string. >> >> I tried this code: >> >> "as sd ".sub(/<[\s\S]*?>| /,"*") >> >> It just replaces the 1st occurance. > > Try: > > ...gsub... > > Hope that helps. > > James Edward Gray II It's also worth pointing out that [\s\S] means a character class containing all whitespace and non whitespace characters--in other words: everything. This is equivalent: "as sd ".sub(/<.*?>| /,"*") which replaces [\s\S] with . Also, if your tags could have line breaks in them, you can add 'm' (for multiline) to the end of the regular expression literal and then . will match newlines as well: /regex/m Oh, and if you replace that last space (after the pipe character) with \s, then it will match tabs too (or any whitespace for that matter). Tom -- Tom Werner Helmets to Hardhats Software Developer tom@helmetstohardhats.org www.helmetstohardhats.org