/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] RegExp is not working right…

Hello,

I’m trying to use the following pattern with RegExp, but it doesn’t seem to work right. When I take the same expression and use it directly, the expression works correctly.

The expression is designed to find numbers or numbers with letter at the end.
The example below should not find any match in the string.
The RegExp example finds “ld”. Why? What am I doing wrong?

[code=html]var str = “printf(“Hello World”);”;

var myPattern = new RegExp(“([^w][d]+[w]*)|(^[d]+[w]*)”);
alert(str.search(myPattern));

alert(str.search(/([^w][d]+[w]*)|(^[d]+[w]*)/));[/code]

Thank you.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NedalsJan 03.2009 — <i>
</i>alert(str.search(/^d+w$/));

^ begining of string
d+ one or more numbers
w one number or letter only
$ END OF STRING
Copy linkTweet thisAlerts:
@the_dpauthorJan 03.2009 — <i>
</i>alert(str.search(/^d+w$/));

^ begining of string
d+ one or more numbers
w one number or letter only
$ END OF STRING
[/QUOTE]

I'm sorry that's not what I asked.

My expression, when used through RegExp doesn't work like it should, and when used directly with / / it works great, why???
Copy linkTweet thisAlerts:
@NedalsJan 03.2009 — Hmm - I guess 'myPattern' does not interpolate within an alert
<i>
</i>var str = "printf("Hello World");";
var myPattern = new RegExp("([^w][d]+[w]*)|(^[d]+[w]*)");

var result = str.search(myPattern);
alert(str.search(result));

Just for reference,

Your regex will match a lot more than ONLY numbers follows by one letter or number.
Copy linkTweet thisAlerts:
@mrhooJan 03.2009 — When you create a RegExp from a string with new RegExp('string') you have to be careful to [B]escape [/B]the [B]escapes[/B].

var myPattern = new RegExp("([^[B]w[/B]][[B]d[/B]]+[[B]w[/B]]*)|(^[[B]d[/B]]+[[B]w[/B]]*)")

Otherwise, the RegExp you create is: /([^w][d]+[w]*)|(^[d]+[w]*)/;// from alert(myPattern)

So you returned the first character that is not a 'w' and is followed by some number of 'd's followed by zero or more 'w's- 'ld'.
Copy linkTweet thisAlerts:
@the_dpauthorJan 03.2009 — When you create a RegExp from a string with new RegExp('string') you have to be careful to [B]escape [/B]the [B]escapes[/B].

var myPattern = new RegExp("([^[B]w[/B]][[B]d[/B]]+[[B]w[/B]]*)|(^[[B]d[/B]]+[[B]w[/B]]*)")

Otherwise, the RegExp you create is: /([^w][d]+[w]*)|(^[d]+[w]*)/;// from alert(myPattern)

So you returned the first character that is not a 'w' and is followed by some number of 'd's followed by zero or more 'w's- 'ld'.[/QUOTE]


Of course....Thank you.
×

Success!

Help @the_dp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...