matching strings starting with (

  • Thread starter Thomas 'PointedEars' Lahn
  • Start date
T

Thomas 'PointedEars' Lahn

jc said:
The regex in this Javascript is not working to find strings that
start with "(". Escaping the ( and using chr(40) did not work either.

There is no built-in chr().
Nor did double escaping it.

It should work if you lose the chr().

<http://jibbering.com/faq/#posting>
I confirmed strings that match are being sent. And if I change my
match to another character like "-" it works.

How do I do this?


if (strTitle.match("^"+"("))

if (strTitle.match("^\\("))

or

if (strTitle.match(/^\(/))

,-[ECMAScript Language Specification, Edition 3 Final]
|
| 15.5.4.10 String.prototype.match (regexp)
|
| If regexp is not an object whose [[Class]] property is "RegExp",
| it is replaced with the result of the expression new RegExp(regexp).
| Let string denote the result of converting the this value to a string.
| Then do one of the following:
| [...]

Also, once past this, how do I remove (somechangingtext) from the
begging of a string?

s = s.replace(new RegExp(expr));

You will need to escape those characters in `expr' that should not have
their default RegExp meaning, though.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
s = s.replace(new RegExp(expr));

s = s.replace(new RegExp(expr), replacement);
You will need to escape those characters in `expr' that should not have
their default RegExp meaning, though.

(RTFM)


PointedEars
 
J

jc

The regex in this Javascript is not working to find strings that
start with "(". Escaping the ( and using chr(40) did not work either.
Nor did double escaping it.

I confirmed strings that match are being sent. And if I change my
match to another character like "-" it works.

How do I do this?


if (strTitle.match("^"+"("))
{
alert("found");



}


Also, once past this, how do I remove (somechangingtext) from the
begging of a string?


Thanks.
 
L

Lasse Reichstein Nielsen

jc said:
The regex in this Javascript is not working to find strings that
start with "(". Escaping the ( and using chr(40) did not work either.
Nor did double escaping it.

I confirmed strings that match are being sent. And if I change my
match to another character like "-" it works.

How do I do this?


if (strTitle.match("^"+"("))
{
alert("found");
}

Thomas has already showed you how to do this correctly with a RegExp.
For so simple a test, I'd prefer just doing it directly:
if (strTitle.charCodeAt(0) == 0x28) {
alert("found");
}
Also, once past this, how do I remove (somechangingtext) from the
begging of a string?

This might do it (remove somechangingtext from the beginning of a
string):
string = string.substring(somechangingtext.length);
It does assume that the string actually starts with the changing text.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
For so simple a test, I'd prefer just doing it directly:
if (strTitle.charCodeAt(0) == 0x28) {
alert("found");
}

Directly (and slightly more compatible) would be

if (strTitle.charAt(0) === "(")

It might even be more efficient as the character value does not need to be
determined externally, no matter the more efficient strict comparison.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Directly (and slightly more compatible) would be

if (strTitle.charAt(0) === "(")

I doubt there are any browsers still alive that doesn't support charCodeAt
(but it's true that it didn't always exist).

My thought was rather that the charCodeAt doesn't need to
(potentially) create a new string object and do string comparison, but
can just return and compare with a simple integer.

However, it's not clearly better if you look at different browsers:

Firefox 3.5: charAt is ~33% faster.
Opera 10: charCodeAt is ~5% faster.
IE8(x64): charCodeAt is ~75% faster.
Chrome 4: charCodeAt is ~110% faster.
Safari 4: charCodeAt is ~85% faster.

This was measured using:

(function() {
var c = "(oglabogla)";
var N = 1000000; // Vary number for slower browsers.
var t0 = new Date();
for (var i = 0; i < N; i++) {
var eq1 = c.charAt(0) === "(";
}
var t1 = new Date();
for (var i = 0; i < N; i++) {
var eq1 = c.charCodeAt(0) === 0x28;
}
var t2 = new Date();
var d = (t1-t0)/(t2-t1); // speed of charCodeAt relative to charAt
return d;
})()

Firefox clearly stands out. I haven't checked 3.6b3.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top