473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp Object property LastIndex


Given the following code and its execution to the last line:

var re = /beta/;
var string = "alphabetagamma ";
var report = re.exec(string) ;

should not:

re.lastIndex = 9;

after the last line of execution?

On Firefox, Venkman continually reports zero.

Specification for JS 1.5 defines property lastIndex as follows:

The index at which to start the next match.
As of JavaScript 1.5, [it is] a property of a
RegExp instance, not the RegExp object.

Sep 6 '06 #1
2 1277
Patient Guy wrote:
var re = /beta/;
var string = "alphabetagamma ";
var report = re.exec(string) ;

should not:

re.lastIndex = 9;

after the last line of execution?
No. The lastIndex property is only changed if the global flag is set
(that is, /beta/g).

The index property of the result array indicates where the matched
substring starts; in the example above, report.index will evaluate to 5.

[snip]

Mike
Sep 6 '06 #2


Patient Guy wrote:
Given the following code and its execution to the last line:

var re = /beta/;
var string = "alphabetagamma ";
var report = re.exec(string) ;

should not:

re.lastIndex = 9;

after the last line of execution?
Use the g flag e.g.
var re = /beta/g;
if you want the exec method to report lastIndex and start from there on
the next call.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2177
by: Magnus Lie Hetland | last post by:
I'm working on a project (Atox) where I need to match quite a few regular expressions (several hundred) in reasonably large text files. I've found that this can easily get rather slow. (There are many things that slow Atox down -- it hasn't been designed for speed, and any optimizations will entail quite a bit of refactoring.) I've tried to speed this up by using the same trick as SPARK, putting all the regexps into a single or-group in...
1
1742
by: Pavils Jurjans | last post by:
Hallo, I yesterday was browsing the book, JavaScript The Definitive Guide (4th ed), which is, unquestionably, the best reference book for JS. To my surprise, I didn't find any documentation about the static properties of global RegExp object, ie, RegExp.lastMatch, RegExp.leftContext, RegExp.rightContext, and all those RegExp.$x properties. I lloked up the ECMA-262 PDF and to my surprise realized that they are not included into the...
3
2683
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null, as every second call to .test() fails. <script> var str = "RegExp (chokes) on this every 2nd time"; var re = /chokes/g;
2
4744
by: brad | last post by:
Hello all, I'm new to javascript--not too new to a few other programming languages--and I need your help deciphering the Regexp in the following string. Regular expresions are hard enough in Python, and since I am new to javascript they are even harder. Well here's the string, thanks for any and all help I receive. document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)
7
3439
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text, re, 3));
11
2920
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g; document.write( ); </script></body></html> ---------------------
6
2265
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
2
14662
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest response. A very strange effect (and I can't find where I've done something wrong) is that regexp matches in this function fail on every second call.
2
5911
by: chris fellows | last post by:
In VS2005 (C#) I want to set the properties of an object dynamically at runtime from an XML configuration file but without having to know the property name when writing the code. The properties are user-defined classes so I need to be able to iterate through the properties of each sub-object and set the property accordingly. For example, Instead of having to write the code MyObject.SubObjectOne.MyStringProperty = "Some text" I will...
0
8444
marktang
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...
1
8551
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,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
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...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.