ID:2273030
 
(See the best response by Ter13.)
Code:


Problem description:

I have an rp verb in a game being developed and within players are allowed to change their personal color for their text. Im trying to change it so that their personal color only exists within quotations when roleplaying so that it is easy to define not only action from words but to also quickly identify a difference in whos saying and doing what. Ive tried to change the rp color but now its just made rp color a standard flat. Is there a special value to give things within quotations? Example: The boy noticed a new being a approached in a subtle manor "hello!" He expressed in excitement. This all appears red currently as ive changed the rp color value. Is there a special way to go about giving the "hello" the players personal color? Am i over analyzing it? Could anyone explain how i would go about giving the above explained? I would greatly appreciate it
Best response
Regex makes this a lot easier to do, but harder to understand.

var
regex/quote = new/regex("(?:\\x22)(\[^\\x22]*)(?:\\x22|$)","g")
proc
quotify(msg,style)
return quote.Replace(msg,"\"<span class='rp' style='[style]'>$1</span>\"")



Regex was recently added to BYOND in the 500 series. It's a framework for working with text in the abstract.

When you create a regular expression, you use a really complex string of characters to describe text matching. The tokens available to you are listed in the reference here:

http://www.byond.com/docs/ref/info.html#/{notes}/regex

This one is pretty simple as far as regex patterns go. it looks for a double-quote (ASCII character hex 0x22 / dec 34) with any number of non-quotes in between followed by a single double-quote, or the end of the text.

\\xXX = ascii character XX (hex)
(?:X) = do not capture X
(X) = capture X
[^X] = anything that is NOT X
X* = any number of X
X|Y = either X or Y

(?:\\x22) = opening quote
(\[^\\x22]*) = anything NOT a quote (any number)
(?:\\x22|$) = closing quote OR the end of the string


The above breakdown sort of describes how the pattern captures text. The "g" flag makes the pattern a global pattern, which allows us to use it to replace all instances in a text string instead of just one.

Last, when we call regex.Replace() on the quote regex datum, we use $1 to pull from the capture groups from the regex pattern. We're adding an HTML tag called a span to each instance that allows us to use a CSS stylesheet to determine the styling of the text with the class property, as well as adding individual style to the string using the style property of the tag.


So how you would use this:

var
regex/quote = new/regex("(?:\\x22)(\[^\\x22]*)(?:\\x22|$)","g")
proc
quotify(msg,style)
return quote.Replace(msg,"\"<span class='rp' style='[style]'>$1</span>\"")
mob
var
rpcolor = "#0000FF" //blue
verb
rp(msg as text)
view(6) << "<span class='rp'>[src] [quotify(msg,"color:[rpcolor];")]</span>"


You should note that I'm heavily using CSS classes in this example code. You can take advantage of CSS in your interface by using the style tab in the interface editor on whatever element this text will be displayed in:



The style tag in the span that encases the color will override the CSS class's style from that box, allowing you to specify global properties in that box, while editing specific properties using the style tag on the fly.
Your the man my dude. Much appreciated. Ive been searching for related topics for days
Regex is hard to wrap your brain around at first. Here's a useful website for working with your own regex patterns. You'll have to go through and escape them for use in DM, but this website was an absolute godsend for me learning to use regex:

https://regex101.com/
I've been using regex for a decade and still don't understand it.