– Common Function Library Project

RESplit(str, regex)

Last updated January 29, 2002


Raymond Camden

Version: 2 | Requires: CF5 | Library: StrLib

This UDF will split a string into an array based on the regular expression passed in. If the regex is not found, the entire string is returned in the first element of the array.

Return Values:
Returns an array.


<cfset str = "This is the end of the sentence. This is the second sentence, it's not quite as interesting.">
<cfset res = RESplit(str,"[\.[:space:],]+")>
<cfdump var="#res#">


Name Description Required
str The string to search. Yes
regex The regular expression to split on. Yes

Full UDF Source:

 * Splits a string into arrays based on a regex.
 * Fix for missing end item by Thomas Muck (
 * @param str 	 The string to search. 
 * @param regex 	 The regular expression to split on. 
 * @return Returns an array. 
 * @author Raymond Camden ( 
 * @version 2, January 29, 2002 
function RESplit(str,regex) {
	var results = arrayNew(1);
	var test = REFind(regex,str,1,1);
	var pos = test.pos[1];
	var oldpos = 1;
	if(not pos) results[1] = str;
	while(pos gt 0) {
		oldpos = pos+test.len[1];
		test = REFind(regex,str,oldpos+1,1);
		pos = test.pos[1];
        //Thanks to Thomas Muck
        if(len(str) GT oldpos) arrayappend(results,right(str,len(str)-oldpos + 1));
	return results;
blog comments powered by Disqus


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson