I'm trying to split a string based upon the middle dot. For some reason, it's not recognizing it. I can use a "pipe" character so I know everything else is working, but not the middle dot. Do I need to escape the middle dot? If so, how?
Javascript
var sTags = $(this).text();
// alert(sTags);
sSplitTags = sTags.split(' | ');
// Split isn't recognizing the middot
// var sSplitTags = sTags.split(' · ');
// var sSplitTags = sTags.split(' · ');
// var sSplitTags = sTags.split(' · ');
alert(sSplitTags[0]);
HTML
<div>Upscale · Gadget</div>
<div>Expensive · Widget</div>
<div>Expensive · Widget</div>
Thanks in advance!
I'm trying to split a string based upon the middle dot. For some reason, it's not recognizing it. I can use a "pipe" character so I know everything else is working, but not the middle dot. Do I need to escape the middle dot? If so, how?
Javascript
var sTags = $(this).text();
// alert(sTags);
sSplitTags = sTags.split(' | ');
// Split isn't recognizing the middot
// var sSplitTags = sTags.split(' · ');
// var sSplitTags = sTags.split(' · ');
// var sSplitTags = sTags.split(' · ');
alert(sSplitTags[0]);
HTML
<div>Upscale · Gadget</div>
<div>Expensive · Widget</div>
<div>Expensive · Widget</div>
Thanks in advance!
Share Improve this question edited Oct 2, 2012 at 20:01 user1003757 asked Oct 2, 2012 at 19:54 user1003757user1003757 412 silver badges8 bronze badges 3-
1
is there supposed to be some delimiters in your code? i see
sSplitTags = sTags.split( | );
, which would be a syntax error. – jbabey Commented Oct 2, 2012 at 19:57 -
Maybe try
sTags.split(' \u00B7 ')
? – ruakh Commented Oct 2, 2012 at 19:58 - jababey, you are correct. I've edited the sample, but that is not the issue. It's just an oversight on my part as I was trying different approaches. ruakh, you're solution works! Thanks everyone for your help! – user1003757 Commented Oct 2, 2012 at 20:14
2 Answers
Reset to default 7' \267 '
Should be what you're looking for. Javascript uses an octal encoding for special characters that differs from the html/css encoding syntax.
Here's a site with a nice lookup table: http://www.evotech/blog/2007/04/named-html-entities-in-numeric-order/
-EDIT-
ruakh brought up this version of ' \u00B7 '
which is also just as valid. You'll notice on the page that there is a column for hex codes and for middle dot it is B7
. In css you would write \00B7
but in javascript we need to qualify that this is not an octal by appending the little 'u' to the front and you'd end up with \u00B7'
.
The code works OK when the character encoding of the document containing the JavaScript code has been properly declared. For example, it works when the code appears, as UTF-8 encoded, inside an HTML document which is UTF-8 encoded as a whole and this encoding is adequately declared. For an external JavaScript file, the HTTP headers should be used to declare the encoding. See the W3C document on character encodings: http://www.w3/International/O-charset.en.php