Regex
paket "regex";
The regex
package provides support for regular expressions.
The package includes:
- Regex constructor
A Regex object contains the following properties:
- sablon
- pronadji
- pronadjiSve
- zamijeni
- potpunoPoklapanje
A Match object contains the following properties
- vrijednost
- lokacija
- pocetak
- kraj
Regex constructor
The Regex constructor returns a Regex object.
- takes:
- pattern - a regex pattern (
tekst
)
- pattern - a regex pattern (
- returns: Regex object (
objekat
) - special cases:
- Throws exception if
pattern
is not a valid regex pattern
- Throws exception if
var regex = Regex("\\d+");
var regex = Regex("Definitely not valid regex");
Error: 'Definitely not valid regex' is not a valid regex pattern.
sablon
The sablon
property is a tekst
value containing the regex pattern of the Regex object.
var regex = Regex("\\d+"); ispis(regex.sablon);
\d+
pronadji
The pronadji
function returns the first match of a regular expression in the provided input tekst
.
- takes: input - string to perform lookup on (
tekst
) - returns: Match object (
objekat
) - special cases
- returns
nedefinisano
if no match is found
- returns
var regex = Regex("\\d+"); var text = "lorem 19 ip5um dol0r s1t am3t"; var match = regex.pronadji(text); ispis(match.vrijednost);
Output: 19
A case where no match is found:
var regex = Regex("[\\[\\]{}()]"); var text = "lorem 19 ip5um dol0r s1t am3t"; var match = regex.pronadji(text); ispis(match.vrijednost);
Output: nedefinisano
pronadjiSve
The pronadjiSve
function returns all matches of a regular expression in the provided input tekst
.
- takes: input - string to perform lookup on (
tekst
) - returns: Array of Match objects (
niz<objekat>
) - special cases
- returns empty
niz
if no matches are found
- returns empty
var regex = Regex("\\d+"); var text = "lorem 19 ip5um dol0r s1t am3t"; var matches = regex.pronadjiSve(text); matches.zaSvaki(funkcija(m) => ispis(m.vrijednost));
Output: 19 5 0 1 3
zamijeni
The zamijeni
function Replaces all occurrences of this regular expression in the specified input string with specified
replacement expression.
- takes: input - string to perform replacements on (
tekst
), replacement - the expression to replace found matches with (tekst
) - returns: new
tekst
with the replacements - special cases
- returns
input
intact if no matches are found
- returns
var regex = Regex("\\d+"); var text = "lorem 19 ip5um dol0r s1t am3t"; var matches = regex.pronadjiSve(text); var t = regex.zamijeni(text, "DIGITS"); ispis(t);
Output: lorem DIGITS ipDIGITSum dolDIGITSr sDIGITSt amDIGITSt
potpunoPoklapanje
The potpunoPoklapanje
function
- takes: input - string to perform lookup on (
tekst
) - returns:
- A Match object if
input
matches pattern in its entirety (objekat
) nedefinisano
otherwise
- A Match object if
- special cases: none
var regex = Regex("\\d+"); var text = "2023"; var match = regex.potpunoPoklapanje(text); ispis(match.vrijednost);
Output: 2023
var regex = Regex("[\\[\\]{}()]"); var text = "2023"; var match = regex.potpunoPoklapanje(text); ispis(match.vrijednost);
Output: nedefinisano
The Match object
The Match object has the following properties:
- vrijednost - the matched string (
tekst
) - lokacija (
objekat
)- pocetak index where match starts (
broj
) - kraj index where match ends (
broj
)
- pocetak index where match starts (
var regex = Regex("\\d+"); var text = "lorem 19 ip5um dol0r s1t am3t"; var match = regex.pronadji(text); ispis(match);
Output
{
vrijednost: "19",
lokacija: {
pocetak: 6
kraj: 7
}
}