Why this code doesn't work?

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.

This code also doen’t work:

"as sd “.sub(/<[\s\S]?>| /){|s| "”}

How can I do that?

Thanks,
Alan

On Jun 21, 2006, at 1:21 PM, Alan M. 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 G. II

James Edward G. II wrote:

It just replaces the 1st occurance.

Try:

…gsub…

Hope that helps.

James Edward G. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs