Hi,
I'm looking for an optimal javascript function to split a camelcase
string and return an array.
I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
Thanks. 10 11953
"pantagruel " <ra************ *@gmail.comwrot e in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,
I'm looking for an optimal javascript function to split a camelcase
string and return an array.
I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
This was a fun little morning brain warmer. ;^)
I've created a function that, I think, will do what you want. It's not that
pretty but it seem to work. In my example I've added it to String prototype
(making it available to all strings) but you can put it anyplace you like...
you could also condense it down to one line of code and append it to any
string but don't do that: functions should be used to abstract ugly code
like this. ;^)
Here it is:
String.prototyp e.CamelCaseToAr ray = function() {
// Preceed Uppercase (or sets of) with commas then remove any
leading comma
var Delimed = this.replace(/([A-Z]+)/g, ",$1").repl ace(/^,/, "");
// Split the string on commas and return the array
return Delimed.split(" ,");
};
Basically this does a regular expression search for uppercase letters (or
sets of uppercase letters) and preceeds them with commas. Since the string
may now have a comma as it's first character another replace is done to
remove it (if it exists). Then the whole mess is split on the commas and
returned.
Here's an example of how you might use it:
TestString = "MyCamelCASEStr ing";
alert(TestStrin g.CamelCaseToAr ray());
This function will (I hope obviously) have problems if there are commas in
the string to begin with. I've tested it on a handful of strings but you
should vette it more before trusting it.
I think (since I'm only caring to look for uppercase characters) that it
should be blissfully unfazed by numbers and punctuation (other than the
comma).
Hope this helps,
Jim Davis
pantagruel wrote:
Hi,
I'm looking for an optimal javascript function to split a camelcase
string and return an array.
I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
Thanks.
"CamelCaseStrin g".match(/[A-Z][a-z]*/g)
returns array:
("Camel", "Case", "String").
Seems like what you want :-)
Jeremy
"Jeremy" <je****@pinacol .comwrote in message
news:%F7wg.1463 2$Nv.7608@fed1r ead10...
pantagruel wrote:
>Hi,
I'm looking for an optimal javascript function to split a camelcase string and return an array.
I suppose one could loop through the string, check if character is uppercase and start building a new word to add to the array but that seems incredibly wasteful. must be some easy way to do it.
"CamelCaseStrin g".match(/[A-Z][a-z]*/g)
returns array:
("Camel", "Case", "String").
Seems like what you want :-)
But this:
"camelCaseStrin g".match(/[A-Z][a-z]*/g)
Returns: ["Case", "String"].
And this:
"camelCaseSTRIN G".match(/[A-Z][a-z]*/g)
Returns: ["Case", "S", "T", "R", "I", "N", "G"]
So... well... neener neener neener. ;^)
(Yeah... I'm a big jackass.)
Jim Davis
>
Here's an example of how you might use it:
TestString = "MyCamelCASEStr ing";
alert(TestStrin g.CamelCaseToAr ray());
Haven't tested yet but on appearance looks like it will work with the
strings I have to deal with, upper and lower camel cased strings that
need to be split into words. Since there is no punctuation allowed in
the strings I can't see this as being a problem.
Thanks for the help, will credit you in the comments of the script.
Cheers,
Bryan Rasmussen
Jim Davis wrote:
>
But this:
"camelCaseStrin g".match(/[A-Z][a-z]*/g)
Returns: ["Case", "String"].
And this:
"camelCaseSTRIN G".match(/[A-Z][a-z]*/g)
Returns: ["Case", "S", "T", "R", "I", "N", "G"]
So... well... neener neener neener. ;^)
(Yeah... I'm a big jackass.)
Jim Davis
Those aren't camel case strings, now are they? :-) You can't expect any
algorithm that targets camel case strings to work on non-camel-case strings.
Jeremy
Jeremy wrote:
Jim Davis wrote:
>> But this:
"camelCaseStri ng".match(/[A-Z][a-z]*/g)
Returns: ["Case", "String"].
And this:
"camelCaseSTRI NG".match(/[A-Z][a-z]*/g)
Returns: ["Case", "S", "T", "R", "I", "N", "G"]
So... well... neener neener neener. ;^)
(Yeah... I'm a big jackass.)
Jim Davis
Those aren't camel case strings, now are they? :-) You can't expect any
algorithm that targets camel case strings to work on non-camel-case
strings.
Jeremy
Besides, by tweaking the regex a little you can account for
lower-camel-case:
"lowerCamelCase ".match(/[A-Z]?[a-z]+/g)
returns {"lower", "Camel", "Case"}
Anything else (i.e. "camelCaseSTRIN G") is not a camel case string. I
would argue that in this case STRING must be an acronym and there's no
reason it should come back in all one string in the result array.
So, strictly speaking... neener neener :-)
Jeremy
JRS: In article <ks************ *************** ***@giganews.co m>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang. javascript, Jim
Davis <ne********@vbo ston.composted :
>"Jeremy" <je****@pinacol .comwrote in message news:%F7wg.146 32$Nv.7608@fed1 read10...
>pantagruel wrote:
>>I'm looking for an optimal javascript function to split a camelcase string and return an array.
>"CamelCaseStri ng".match(/[A-Z][a-z]*/g)
returns array:
("Camel", "Case", "String").
Seems like what you want :-)
But this:
"camelCaseStri ng".match(/[A-Z][a-z]*/g)
Returns: ["Case", "String"].
But this : "camelCaseStrin g".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case', 'String']
And this : "camelCaseSTRin g".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case', 'STRing']
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton wrote:
>
But this : "camelCaseStrin g".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case', 'String']
And this : "camelCaseSTRin g".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case', 'STRing']
What does \b do? Never seen that before.
Jeremy
"Dr John Stockton" <jr*@merlyn.dem on.co.ukwrote in message
news:Ar******** ******@merlyn.d emon.co.uk...
JRS: In article <ks************ *************** ***@giganews.co m>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang. javascript, Jim
Davis <ne********@vbo ston.composted :
>>"Jeremy" <je****@pinacol .comwrote in message news:%F7wg.14 632$Nv.7608@fed 1read10...
>>pantagruel wrote:
>snip<
But this : "camelCaseStrin g".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case', 'String']
And this : "camelCaseSTRin g".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case', 'STRing']
No - that doesn't count. You didn't say "neener neener". ;^)
Jim Davis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
|
by: John Benson |
last post by:
I never cared for CamelCase because a lot of words in English are compounds,
and remembering the right CamelCase rendition of them gets difficult. For
example, an object attribute named
HaveAMerryChristmas
isn't too hard, but what about deciding between
HaveAGoodWeekend
|
by: Vamsi |
last post by:
Hi,
I am trying a basic opearation of splitting a multiline value to an
array of single lines(Actually making Address into AddressLine1,
AddressLine2).
I used Environment.NewLine in split, I could get only 1st line, but it
is not returning 2nd line.
here's code:
string address = null;
string ssep = Environment.NewLine;
char sep = ssep.ToCharArray();
|
by: John Salerno |
last post by:
This is an example in the book I'm reading:
string fullName = " Edward C Koop ";
fullName = fullName.Trim();
string names = fullName.Split(' ');
string firstName = names; // Edward
Two questions about this:
1. Why do you use single quotes with Split() instead of double? Is this
|
by: Dave |
last post by:
Greetings,
Is there a way I can split a string into an array on a space OR a carriage
return? What would the code look like for this?
Thanks,
-Dave
| |
by: Ben |
last post by:
Hi
I am creating a dynamic function to return a two dimensional array from a
delimeted string.
The delimited string is like:
field1...field2...field3...
field1...field2...field3...
field1...field2...field3...
|
by: Saurabh |
last post by:
Hi everyone,
I am looking for some expert advise to get me out of trouble. I am looking for a solution in C# which will allow me to split the below string in the format provided. The String.Split() allows only a single char as a delimiter.So I cannot use that as well.
Original String
"*** MASTER (Supervisor) *** September 23, 1997 at 3:22pm \r\nThis is the first line of text. There will be many more beneath this on so look...
|
by: pmwhelan |
last post by:
Hi
I want to split a string into an array and treat quoted phrases as words.
I was splitting on a space which worked fine.
char delimiterChars = {' '};
string arrValues = searchText.Split(delimiterChars);
This would split a sentence ...
this is a test
|
by: shapper |
last post by:
Hello,
I have a string as follows:
string text = "Paris, New York , London,Lisbon,Oslo, Chicago ";
I need to get all words with no commas or spaces:
"Paris", "New", "York", "London", "Lisbon", "Oslo", "Chicago"
I am using (I think it is ok):
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |