In Javascript, there are multiple techniques for parsing strings. While regular expressions are the most advanced method, simpler tasks can be accomplished using functions like substr
and split
.
split
breaks a string into parts, as demonstrated by:
".a {color: red; background: blck}".split('{')
This would create an array with two segments, one before "{" and one after:
[".a ", "color: red; background: blck}"]
Alternatively, substr
allows you to extract portions of a string. For example:
".a {color: red; background: blck}".substr(4)
This function excludes the first four characters:
"color: red; background: blck}"
On the other hand:
".a {color: red; background: blck}".substr(4, 10)
skips the initial four characters, includes the following ten, and then ignores the rest:
"color: red"
To explore more string prototype methods, visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String