With Javascript how would I search the first three letters in a string and see if they match "ABC"? Thanks
views:
46answers:
3
Q:
With Javascript how would I search the first three letters in a string and see if they match "ABC"?
+4
A:
Using a regular expression:
str.match(/^ABC/);
or using the substring
method:
str.substring(0, 3) == 'ABC';
Sinan Ünür
2009-08-12 17:40:54
Hi Sinan, it is saffer to use first method?
SourceRebels
2009-08-12 17:58:30
'Safer' in what sense? Security-wise, I do not see a problem with either. If we are talking about the availability, I believe `substring` has been available since JS 1.0 and `match` was added later in JS 1.2.
Sinan Ünür
2009-08-12 18:08:07
The first method is slower. I don’t know what you mean by “safer” though.
Nate
2009-08-12 18:09:23
For such a simple need, its actually better to use the substring method, as it is significantly faster.
seanmonstar
2009-08-12 18:09:36
@seanmonstar and @Nate agreed.
Sinan Ünür
2009-08-12 18:13:51