Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting on 2 characters instead of one 2

Status
Not open for further replies.

Rekcor

Programmer
Feb 15, 2005
48
NL
I've got a string:

"In the British-Commonwealth in some cases"

I want to split this string up in words, but also want to split on -, so I get one array:

Array[0] "In"
Array[1] "the"
Array[2] "British-"
Array[3] "Commonwealth"
Array[4] "in"
Array[5] "some"
Array[6] "cases"

How to do this? Im familar with the Split()-method, but it only accepts one character/string to split on.
 
I don't know if that particular string is the exact problem you're trying to work around, or if it is simply an example... but if the former, why would you hyphenate "British-Commonwealth"?

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
The split() function also takes a re like:

Code:
<script type="text/javascript">
var str = "In the British-Commonwealth in some cases";

function makeArray(s){
var re = /\s|-/;
var strArray = s.split(re);
	for(var j = 0; j < strArray.length; j++){
		alert(strArray[j]);
	}
}

makeArray(str);

</script>

Check out this reference:
split()

Thanks,
--Mark
 
@Dan
Its just an example. I'm making a wrapping tool which automatically breaks of words with an '-'.

@Marks

Thanx! Maybe next time I should use a more advanced JS reference (mine didn't the re option)

You've got my star!
 
Glad to help, Rekcor.

Appreciate the star/s.

Thanks,
--Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top