/    Sign up×
Community /Pin to ProfileBookmark

Searching A String

I’m having a little trouble with searching a string for something. I am currently using the ‘String.search(“This Is A String”);’ method.

But I need to search a string for the right parenthasis character “)”. Just typing it in between the quotation marks doesn’t work. I tryed to make a string var that holds a string { var AString = “‘);” }. This doesn’t work though because the following code { String.search(AString); }.

That’s not exactly the wording for my document, but it is the exact same concept, and almost identical except I use different var names.

Is there any other way to search a string so I can find the parenthasis?

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Jonny_LangJul 29.2005 — .....
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorJul 29.2005 — Thank you.

I don't quote understand the sytax of that function though... What if I wanted to return the string between (' and ')? how would that go into that function?
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorJul 29.2005 — And also, is there a way to use the .match() method to find a string between two characters(or two sets of characters), and then do it again, but continue where you left of?

var String = "This is (return this part) a sentence or string (then return this) in javascript.";

Then return the first string in parenthasis, and use the method again to also return the second one(like a counting type of method along with this. when using String.match(/+(.*)+/); it would return 'return this part'. then if it were used again in the same function it would return 'then return this').
Copy linkTweet thisAlerts:
@MongusJul 29.2005 — In regular expressions parenthesis indicate grouping so you have to escape them if that's what you're searching for. You should read up on regular expressions. They might hurt your brain to begin with but they're well worth learning.

It sounds like if the given string is "this is a ('test')" you want the function to return "test", correct? If so, this should work:
<i>
</i>&lt;script type="text/javascript"&gt;
function search(string)
{
var m = string.match(/('(.*)')/);

<i> </i>if (m)
<i> </i> return m[1];
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@Jonny_LangJul 29.2005 — .....
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorJul 29.2005 — Well that seemed to have worked, but the value it returns.... it is "('some part of a string'),some part of a sting". .....Why?(if anyone knows...

Sorry, I figured that out. I didn't use String[1]. Why does it return the found value as a array?
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorJul 29.2005 — Oh, and what if a string had like:

This is ') a string ('that should') return certain sections.

How would it work on that? there is a ') before the first (', so would it use the first one, or the one after ('?
Copy linkTweet thisAlerts:
@Jonny_LangJul 29.2005 — ....

End of the line for me. I have a name and it's not: "anyone."
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorJul 29.2005 — TO: Jonny Lang

Where did I need to address you by name? At first it was just me and you. Then I replyed to the next user's post(Mongus). Also, I seemed to have missed one of your post(#6).

I hope you were being sarcastic or something when you said
And My God, are you too lazy or arrogant to address me by name? I 'literally' despise people like you.[/QUOTE]
Cus I don't see where I would have had a problem with addressing someone. I replyed to you, and you were the only other person to post, then I just saw Mangus' post and replyed to him(and if you read the reply, it would appear to be obvious who I am talking to). Then I just now saw your next two post.

My goodness.... I don't ever believe I refered to you as 'anyone'. When I used anyone, I was asking a general question to any user of this forum who might read my post. If I said 'Jonny', then I would be directing it to you, but I wanted to direct it to anyone who might read my post, hints why I used 'anyone' ?
Copy linkTweet thisAlerts:
@Jonny_LangJul 29.2005 — .....
Copy linkTweet thisAlerts:
@MongusJul 29.2005 — To answer your "anyone" question ? , String.match() returns an array of the matched groups. The first element is the complete string that was matched. The following elements are the groups that matched in order. So at index 1 we have what was between the first set of parenthesis. When you printed it out you saw both elements of the array.

Does that make sense? I'm not sure I explained it very clearly.
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorAug 01.2005 — I think I got it.

But to clear one thing up. So if you had a string that = "This (is a string) that is going to (be used) in a search type (method) called .match()". And you did the match for just parenthasis(left and right), then the array would contain what? all of the results? If it does, then what does the first array item look like?(index 0)
Copy linkTweet thisAlerts:
@MongusAug 02.2005 — It comes up with "(is a string) that is going to (be used) in a search type (method) called .match()" because .* is greedy and will match as much as it can. If you put a question mark after the .* then it becomes non-greedy and matches "(is a string)".
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorAug 02.2005 — wait, so that would be index 0? if so, then would the rest of the array be like

index 1 = "(is a string)"

index 2 = "(be used)"

index 3 = "(method)"

index 4 = ""

? or would there just be an index 1 = "(is a string) that is going to (be used) in a search type (method) called .match()"?
Copy linkTweet thisAlerts:
@MongusAug 02.2005 — If you add a "g" (global) to the end of the regular expression like so:
<i>
</i>var m = "This (is a string) that is going to (be used) in a search type (method) called .match()".match(/((.*?))/g);

You end up with
<i>
</i>m[0] = "(is a string)"
m[1] = "(be used)"
m[2] = "(method)"
m[3] = "()"
which is a list of everything that matched in the entire string.

If you don't add a "g" to the end of the regular expression:
<i>
</i>var m = "This (is a string) that is going to (be used) in a search type (method) called .match()".match(/((.*?))/);

You end up with
<i>
</i>m[0] = "(is a string)"
m[1] = "is a string"
which is a list of the first whole match and the submatches within that whole match.
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorAug 02.2005 — Ok. Thank You Mongus and Thank You Jonny(even though I didn't like your attitude on this topic...)
×

Success!

Help @Sup3rkirby 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.10,
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,
)...