I have a page which contains many tags with a string in them, for example 'Am I a String? Yes'.
Using JQuery, I want to get the first instance of 'Am I a String? ' and then take the Yes or No after that and store it so I can use it in a conditional.
Any ideas?
Thanks.
The code I'm going with:
function experimentThree() {
var stringBool = $('*:contains("Was it enough? ")');
console.log('stringBool = : ' + stringBool);
console.log('stringBool Object 1 = : ' + stringBool[0]);
}
Thinking if I can get the complete string in the first object I can compare that to what I expect to see.
check this out
$('body').find('*:contains("Am I a String")').each(function(index, crntNode){
var parts = $(crntNode).text().split('?');
var flag = parts[1].trim();
alert(flag);
});
for example:
var a = $('div').text().match(/Am I a String\? (Yes|No)/g)[0];
var b = a.match(/(Yes|No)/g)[0];
Related
I have an array with one element
ClickMeArray[0] = "ClickMe=a-6,a-7,a-8,a-9,"
I want variable to return elements after ClickMe= from ClickMeArray.
Output should be a-6,a-7,a-8,a-9,.
ClickMeArray[0].split('ClickMe=')[1];
Try This
var val = "ClickMe=a-6,a-7,a-8,a-9,";
var myString = val.substr(val.indexOf("=") + 1);
There are many way to do this work. One way is using .replace(). You can replace ClickMe= with empty to getting another part of string.
var ClickMeArray = "ClickMe=a-6,a-7,a-8,a-9,";
console.log(ClickMeArray.replace('ClickMe=',''));
I am trying to "clean" a text string that looks something like this:
DATABASE:madsat NL:Show all platforms of Salute generated from NAIs with no go mobility.
The cleaned string should look like this:
Show all platforms of Salute generated from NAIs with no go mobility.
I am trying the following code but it doesn't seem to like it when I pass in a variable as the string gets returned unchanged:
$(document).ready(function(){
$.get('inputQueryExamples.txt',function(data){
var queryString = data;
var cleanString = "";
var db = '';
$('#database-list').change(function(){
db = $('#database-list').val();
// /(^DATABASE:.*\r\n)(^NL.*)/gm
// http://regex101.com/r/mN4hS2
regex = new RegExp('(^DATABASE:'+ db +'\r\n)(^NL.*)' ,'gm');
console.log(db);
console.log(regex);
//put code in here to replace un-needed stuff
$('#what').append(regex + '<br>');
cleanString = queryString.match(regex);
var nlString = cleanString.map(function(el) {return el.replace('DATABASE:' + db + ' NL:','');});
for (i=0; i<nlString.length; i++){
$('#what').append(nlString[i]+'<br>');
}
}); // end change
Any insight into what i am doing wrong will be appreciated. Thanks
So this works, but I am not sure why I have to process the string twice. Can anyone explain?
var nlString = cleanString.map(function(el) {return el.replace('DATABASE:' + db,'');});
nlString = nlString.map(function(el){return el.replace('NL:',''); });
Something like this?
var s = "DATABASE:madsat \r\nNL:Show all platforms of Salute generated from NAIs with no go mobility.";
var db = "madsat";
var r = new RegExp('(^DATABASE:'+ db + '[\\s\\r\\n]*)(^NL:)' ,'gm');
s1.replace(r, "");
//=> "Show all platforms of Salute generated from NAIs with no go mobility."
Update
It took a while for what you're trying to do to sink in (and your sample data was pretty buried in that regex101 link.)
But I think this JSBin is something close to what you want to do: It still does one pass to find the matches, and a second one to remove the unwanted parts of that match. But the second pass is handled by a single regex replace call rather than your double replaces above. (Click the "Run with JS" button and enter "madsat" or "geoquery" in the box.)
This is the relevant code:
$(document).ready(function() {
$.ajax('http://jsbin.com/fase/1.js', {dataType:'text'}).done(function(data){
var $what = $("#what");
$('#database-list').change(function(){
var db = $(this).val(),
base = '(^DATABASE:'+ db + '[\\s\\r\\n]*)(^NL:)';
var regex1 = new RegExp(base + '(.*)' ,'gm');
var regex2 = new RegExp(base, 'gm');
(data.match(regex1) || []).map(function(str) {
return str.replace(regex2, "");
}).forEach(function(query) {
$what.append(query + "<br/>");
});
});
});
});
Note that the two regexes are identical except that the first one matches the remainder of the "NL:"-line, and the second one doesn't.
I'm trying to find a specific character, for example '?' and then remove all text behind the char until I hit a whitespace.
So that:
var string = '?What is going on here?';
Then the new string would be: 'is going on here';
I have been using this:
var mod_content = content.substring(content.indexOf(' ') + 1);
But this is not valid anymore, since the specific string also can be in the middle of a string also.
I haven't really tried anything but this. I have no idea at all how to do it.
use:
string = string.replace(/\?\S*\s+/g, '');
Update:
If want to remove the last ? too, then use
string = string.replace(/\?\S*\s*/g, '');
var firstBit = str.split("?");
var bityouWant = firstBit.substring(firstBit.indexOf(' ') + 1);
im trying to parse the following string;
"data[Product][0][fieldname]"
i need to be able to change the "[0]" part to a number, the "data[Product]" wont change while the "[fieldname]" will change.
im not sure how to do it with javascript but have an rough idea.
all i have which is wrong, is below but since [0] varies each time it doesnt work;
name="data[Product][0][fieldname]";
name.replace('[0]', '['+newrowid+']'
Jsfiddle;
http://jsfiddle.net/fuzzy_dunlop/8WKcs/
Use a regexp:
var a = "data[Product][0][fieldname]";
var re = /(.*\[.*\]\[)(.*)(\]\[.*\])/gi;
newstr = a.replace(re, "$1" + "YourString" + "$3");
document.write("Now: " + newstr);
Example code: http://jsfiddle.net/NKbd9/1/
Use this instead
name = name.replace('[0]', '['+newrowid+']');
Replace can take a regular expression instead of a string.
Try this:
name="data[Product][0][fieldname]";
name.replace(/\[\d\]/, '['+newrowid+']');
I assume you are trying to do this inside a loop, in that case you just need to make it dynamic. Also not that replace doesn't change the current string, it returns a new string so you need to assign the value returned back to name.
var i = 0,
len = 10,
name,
newrowid = 13;
for(; i <len; i++) {
name = "data[Product]["+i+"][fieldname]";
name = name.replace('['+i+']', '['+newrowid+']');
}
everyone. I've got a string looks like
var s = "2qf/tqg4/ad(d=d,s(f)d)"
And I've got another string
var n = "abc = /fd/dsf/sdf/a.doc, "
What I want to do is insert n after the first '('
So it will look like
"2qf/tqg4/ad(abc = /fd/dsf/sdf/a.doc, d=d,s(f)d)"
Just use the replace function:
var result = s.replace("(", "("+n);
This barely needs REs.
var t = s.replace(/\(/, '('+n);
This doesn't need REs at all, as String.replace takes strings as well as REs to specify what should be replaced.
var t = s.replace('(', '('+n);