473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to search a particular substring in square bracket

Hi frnds,

Im having a problem in searching out a particular substring in a
string.

Here the code:
---------------------
<html>
<script>
var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
/manual/en/images/down.gif] [304] [0] [] [396 msecs]";
var ip="[192.168.1.61]";
var arr=tmpString.m atch(ip)
(or)
var arr=tmpString.s earch(ip)

alert(arr);
</script>
<body>
</body>
</html>

So in my tmpString variable, i have the data like this with square
brackets,Now i want to compare the ip variable value with tmpString
contained ip value, due to this sqaure brackets im not able to compare
it accurately.I used "match" and "search" commands, both are not
useful. While using "search" , it is showing error if square bracket
used.using match it is returning some interger.
If im comparing with square brackets, then some other problems are
creating i.e, if i give 192.168.1.6 as ip variable value and compared
with tmpString value, then also it is showing output as the correct
one.

Can anyone give me a proper solution for this

Thanks
Dinesh....

Dec 6 '06 #1
3 2924
ba************* ****@gmail.com wrote:
Im having a problem in searching out a particular substring in a
string.
var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
/manual/en/images/down.gif] [304] [0] [] [396 msecs]";
var ip="[192.168.1.61]";
var arr=tmpString.m atch(ip)
(or)
var arr=tmpString.s earch(ip)

So in my tmpString variable, i have the data like this with square
brackets, Now i want to compare the ip variable value with tmpString
contained ip value, due to this sqaure brackets im not able to compare
it accurately.
Both match and search take regular expressions as their arguments.
And, if given a non-regex argument, match() and search() automatically
_convert_ that argument to a regex.

Square brackets [] are a regular expression operator. In a regex,
"[192.168.1.61]" matches _one_character, _ as long as that character is
_any_ of 1,9,2,6 or 8.
While using "search" , it is showing error if square bracket
used.
Hmm, when I run your example code using search(), arr evaluates to 11.
11 is the position in tmpString of the first match for the regex
"[192.168.1.61]"; which is the digit "1" in "18:25:14".
using match it is returning some interger.
Agreed. Using match() in your example, arr evaluates to 1. In this
case, match() is returning the string that was matched. But the regex
"[192.168.1.61]" only matches one character, the 1 in "18:25:14".

If you escape each of the square brackets in ip with a double
backslash, you should get more reasonable results:

var ip="\\[192.168.1.61\\]";
var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61]
[GET/manual/en/images/down.gif] [304] [0] [] [396 msecs]";
var matched=tmpStri ng.match(ip);
var searched=tmpStr ing.search(ip);

Now matched should contain "[192.168.1.61]", which is the part of
tmpString that matches ip. And searched should contain "26", which is
the position in tmpString of the first character of the substring that
matches ip.

Dec 6 '06 #2
ba************* ****@gmail.com wrote:
Hi frnds,

Im having a problem in searching out a particular substring in a
string.

Here the code:
---------------------
<html>
<script>
var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
/manual/en/images/down.gif] [304] [0] [] [396 msecs]";
var ip="[192.168.1.61]";
var arr=tmpString.m atch(ip)
The paramter supplied to match is supposed to be a regular expression,
not a string. If you just want to see if tmpString contains the string
"[192.168.1.61]" then use test (tmpString wrapped for posting):

var tmpString = "[Wed May 3 18:25:14 2006] [192.168.1.61]"
+ " [GET /manual/en/images/down.gif] [304]"
+ " [0] [] [396 msecs]";
alert( /\[192\.168\.1\.61 \]/.test(tmpString ) );

If you wish to supply the IP address as a variable, you can use:

var ip = "192.168.1. 61";
var re = new RegExp('\\['+ip.replace(/\./g,'\\.')+'\\]');
var tmpString = "[Wed May 3 18:25:14 2006] [192.168.1.61]"
+ " [GET /manual/en/images/down.gif] [304]"
+ " [0] [] [396 msecs]";
alert( re.test(tmpStri ng) );

A regular expression of:

var re = new RegExp('\\[' + ip + '\\]');

may be sufficient, but there is a good chance it many not. Please test
thoroughly.

--
Rob

Dec 6 '06 #3
VK

ba************* ****@gmail.com wrote:
<html>
<script>
var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
/manual/en/images/down.gif] [304] [0] [] [396 msecs]";
var ip="[192.168.1.61]";
var arr=tmpString.m atch(ip)
(or)
var arr=tmpString.s earch(ip)

alert(arr);
</script>
<body>
</body>
</html>
if (tmpString.inde xOf(ip) != -1) {
// match found
}
else {
// no match found
}

KISS rules :-)

Dec 6 '06 #4

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

Similar topics

1
6408
by: Veverita | last post by:
Hi there I'm hoping that someone can help me with a question I have about javascript syntax. I got an html page that uploads an image and some text field to a database. What I'd like to do is modify the content of one of the textfields prior to it being submitted to the database. Specifically, I need to
9
5561
by: Christine | last post by:
It has come to my attention that sometimes, when I open a Query in SQL View, the SQL that I see is not exactly the same as the SQL in the Query's Querydef. The difference I see (only occasionally) has to do with square brackets and dot on some tblName.FieldName portions of the query. The following routine demonstrates how I see the...
32
14776
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
7
1431
by: pyluke | last post by:
I'm parsing LaTeX document and want to find lines with equations blocked by "\", but not other instances of "\" so, in short, I was to match "\" to add to this, I also don't want lines that start with comments. I've tried: check_eq = re.compile('(?!\%\s*)\\\\\)\\\\\[')
5
1216
by: digitalorganics | last post by:
What's the best way to search a string for a particular word and get a booleen value indicating whether it exists in the string or not? Thanks...
4
4892
by: red vertigo | last post by:
HI All, Can't quite get my head aorund this problem. My knowledge or more importantly my experience of using T-SQL is small and as a consequence my stored procedures are basic. My current project involves simple stored_procs searching/retrieving Legacy data. Key problem is that in the case of the 'Job' table, the 'Job Number' is an...
6
2665
by: MLH | last post by:
I'm sure its a bozo question, but it's got me stumped. How do I search for a question mark in an open table using CTRL-F to launch the search in the current field. The field is a text field. Not all records have a "?" in the field, but some do. The search process treats the question-mark as a wildcard character and finds the occurrence...
1
5690
by: prodziedzic | last post by:
I want use scanf to read from input everything till right square bracket ']'. It seems to be something like that: scanf("%]) but that's not working. Any ideas?
0
10726
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not...
0
7464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7979
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7497
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...
0
7826
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...
0
6065
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...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5107
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...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
781
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...

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.