ok, so I have this small block of text:
function onfocus(event) {
if ($(this).val() == "Some Arbitrary Text") {$(this).val("");}
}
Using jQuery开发者_如何学运维 or JavaScript, I would like to find teh "Arbitrary Text". This text block is constant, with the exception of the "Arbitrary Text". Ideally, I would like a way to parse it without using complicated loops and regex.
To help clarify: The fact that the text is javascript plays no part. Think of it as just text I am parsing. The "Arbitrary Text" can be anything, I am trying to find the text between the 2 quotes.
Not that I understood the question completely, but maybe
var s = 'foo "quoted" bar';
var m = s.match(/"(.*?)"/);
alert(m[1]); // m[1] = quoted
Of course, this is also possible without regexps, but it would make no sense - this is what regexps are for
var text = $(this).val().replace(/"(.*?)"/ig, "$1");
Your question is a bit confusing - are you looking to see if a large string contains a smaller string? If so, use the indexOf
function like this:
function onfocus(event) {
if ($(this).val().indexOf("Some Arbitrary Text") > 0) {
$(this).val("");
}
}
var stringToSearchFor = 'if ($(this).val() == "';
var haystack = 'put your javascript code here';
var startPosition = haystack.indexOf(stringToSearchFor);
I suppose you are actually displaying some kind of "invite" message inside an input element like, for example: "put your name here" inside a text input. Of course, you want this invite to disappear as soon as the user focuses on that input, and have it reappear if the user didn't put anything in the input. The intention is right, but your approach is a bit dirty and unreliable. I would use variables instead.
Since your inputs are added dynamically, simply add the variable inside a < script> tag right after the html injection. for example:
document.write('<input id="controller24" type="text" name="email" value="Put your name here"/><script>var controller24Default="Put your name here";</script>');
After that, you should bind that controller a generic function that reconstruct the default variable out of the input id value. var thisDefaultValue = eval("controller"+this.id+"Default")
. Just an idea.
If I'm interpreting your question correctly, you just need a regular expression:
astring = "oh hai I'm a string my name is \"Arbitrary Text\"";
results = astring.match(/"[\d\w\s]*"/g);
The regexp /"[\d\w\s]*"/
will match any digit, word or whitespace characters appearing between inverted commas. The suffix g
indicates the search is global, and returns an array of all matches. Removing the g will simply return the first result.
In this case match()
returns a 1-member array: ['"Arbitrary Text"']
. Yes, it includes the inverted commas. To remove them: string = string.replace(/"/g, '');
Null is returned if no match is found in the text.
Good javascript-regexp cheatsheet here.
Edit
Now that we understand your rationale for doing this, I have to recommend against this method.
There are any number of ways to compare a form input's text to its default value. You could store the default value in a global JS variable, for instance, or as an attribute of the html element itself.
That is not to say your method is "wrong", but this code has a serious aroma to it.
精彩评论