Javascript substring check - javascript

I am working with images, they all have their sizes mentioned in their name tag.
http://mysite.com/audio/display%20image/130x130%20jpeg/5755285.jpg
I am getting those is an array of strings. I need to check if the string contains the size
130x130 in it. How can it be done?

var str = 'http://mysite.com/audio/display%20image/130x130%20jpeg/5755285.jpg';
var search = '130x130';
str.indexOf(search);
If it returns anything but -1 the string has been found:
if (str.indexOf(search) > -1) {
// Your image contains 130x130
} else {
// Your image does not contains 130x130
}

Related

Internal Site Search Jquery against a RegExp

Where am I going wrong? Even one error would help please.
I have an HTML input and a submit button. The idea is to:
Submit search string
Get string value.
Compare string value to regex.
If legit, find instances of the string in the DOM.
Then scroll to the first instance of the matched string as it sits in the DOM.
$("#submit").on("click", function () {
//regex to be compared against
var search = new RegExp();
search = /(^\w[A-z]+)$|(^\d[0-9\.x\.X\.m\.M]+)/;
//grab the string value from the search input
var userin = $("#searchin").val();
var compare = userin.test(search);
if (compare === true) {
var treebody = $('html, body').contents().filter(function (userin) {
if ($('html, body').contents() === userin) {
$('html, body').animate({'scrollTop' : $(treebody).position().top}, 700)
} else {
alert("Please search again or scroll down to find your desired content");
}
});
} else {
alert("Sorry, we couldn't match your search. Please try a region or place or a billboard size e.g. 9x13 ");
}
});
Change the line
var compare = userin.test(search);
it should be
var compare = search.test(userin);
Also check you regular expression. Here is a good reference RegEx.

recognize element content and modify dynamically using jquery

I'm injecting text into page using jquery like this
$("#myDiv").text(someWebServiceResponse.Data);
this returned data is in most cases numbers divided with commas but in some cases can be
string as a title which is followed with numbers
Returned data case without title
1,2,3,4,89,11
Returned data case with title
MyTitle 1,2,3,4,89,11
Returned data case with title
MyTitle2 1,2,35,4,89,14
Those title and number values are dynamic ofcourse.
How can I recognize if returned data contains title (maybe using typeofstring) and
modify this returned string with title + and than numbers in next line
Given that the numbers are divided only by , and not by space, you can easily test if the string contains empty space.
Like this
function containsTitle(input) {
return input.indexOf(' ') > 0;
}
...
if (containsTitle(someWebServiceResponse.Data)) {
//TODO: split for 2 lines or whatever you need
}
With your quoted examples, just find the last space if any and insert the plus sign:
var lastSpace = yourString.lastIndexOf(' ');
if (lastSpace != -1) {
yourString = yourString.substring(0, lastSpace) + " +" + yourString.substring(lastSpace);
}
Live Example:
test("1,2,3,4,89,11");
test("MyTitle 1,2,3,4,89,11");
test("MyTitle2 1,2,35,4,89,14");
function test(yourString) {
var lastSpace = yourString.lastIndexOf(' ');
if (lastSpace != -1) {
yourString = yourString.substring(0, lastSpace) + " +" + yourString.substring(lastSpace);
}
snippet.log(yourString);
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
try this:-
var isTitle=function(txt){
var first=txt.substr(0,txt.indexOf(','));
return !$.isNumeric(first);
}
//here how to use
alert(isTitle('1,2,3,4,89,11'));
alert(isTitle('MyTitle 1,2,3,4,89,11'));
Demo

Replacing Stings, Index (-1) value must be greater or equal to zero.?

I'm getting the following error in my app's script when replacing strings in a template file to generate reports.
Index (-1) value must be greater or equal to zero.
The function is listed bellow.
/**
* Search a String in the document and replaces it with the generated newString, and sets it Bold
*/
function replaceString(doc, String, newString) {
var ps = doc.getParagraphs();
for(var i=0; i<ps.length; i++) {
var p = ps[i];
var text = p.getText();
//var text = p.editAsText();
if(text.indexOf(String) >= 0) {
//look if the String is present in the current paragraph
//p.editAsText().setFontFamily(b, c, DocumentApp.FontFamily.COMIC_SANS_MS);
p.editAsText().replaceText(String, newString);
// we calculte the length of the string to modify, making sure that is trated like a string and not another ind of object.
var newStringLength = newString.toString().length;
// if a string has been replaced with a NON empty space, it sets the new string to Bold,
Logger.log([newString,newStringLength]);
if (newStringLength > 0) {
// re-populate the text variable with the updated content of the paragraph
text = p.getText();
Logger.log(text);
p.editAsText().setBold(text.indexOf(newString), text.indexOf(newString) + newStringLength - 1, true);
}
}
}
}
When it errors out
[newString,newStringLength] = [ The Rev Levels are at ZGS 003 on the electric quality standard. The part has a current change to ZGS 005!,108]
Does anyone have any suggestions?
Thanks in advance,
Michael
You are not handling the case where the string isnt there. Thus indexOf returns -1 and you use that. Also dont use reserved words like String for variable names.

Regex split string and check if string is of type

Aanval op Vlemis (499|453) C44
This is what the string looks like. Though it's actually like this: "Aanval op variable (variable) variable
What I want to do is 1: get the coordinates (I already have this), 2 get Vlemis (first variable), get C44 (third variable) and check to see if the string is of this type.
My code:
$("#commands_table tr.nowrap").each(function(){
var text = $(this).find("input[id*='editInput']").val();
var attackername= text.match(/(?=op)[\s|\w]*(?=\()/);
var coordinates = text.match(/\(\d{1,3}\|\d{1,3}\)/);
});
Coordinates works, attackername however doesn't.
Html:
<span id="labelText[6]">Aanval op Vlemis (499|453) C44</span>
You should use one regex to take everything :
var parts = text.match(/(\w+)\s*\((\d+)\|(\d+)\)\s*(\w+)/).slice(1);
This builds
["Vlemis", "499", "453", "C44"]
If you're not sure the string is valid, test like this :
var parts = text.match(/(\w+)\s*\((\d+)\|(\d+)\)\s*(\w+)/);
if (parts) {
parts = parts.slice(1);
// do things with parts
} else {
// no match, yell at the user
}

Javascript say if a folder is child of another

I have these strings which are addresses of files and folder:
../../../folder1/sub1/sub12/
../../../folder1/
../../../another-folder/
I want to compare them using javascript - possibily jquery - to see if for example string 1 have a part egual to string 2 but something more saying that string 1 is child of string 2.
How can i do this?
you could try something like the following
var path1 = "../../../folder1/";
var path2 = "../../../folder1/sub1/sub12/";
if (path2.indexOf(path1) != -1){
//path2 is a sub of path 1
}
In case your string can contain also absolute paths or paths containing .. not only at the beginning I would recommend checking if .indexOf return 0 instead of anything that is not -1.
It can help with cases like.
var path1 = "/rootFolder/";
var path2 = "../folder/rootFolder/";
if (path2.indexOf(path1) === 0) {
console.log("You want this"); // won't get executed => good
}
if (path2.indexOf(path1) !=-1) {
console.log("You don't want this"); // will get executed => bad
}
if(string1.indexOf(string2) != -1){
//string2 is present in string1
}
else{
//string2 is not present in string1
}
You can use the indexOf method to find whether one string is a part of another string.
From w3schools documentation:
The indexOf() method returns the position of the first occurrence of a
specified value in a string.
This method returns -1 if the value to search for never occurs.
var test = "../folder/subfolder1";
var test2 = "../folder";
if (test.indexOf(test2) !=-1) {
alert(test + " is a subfolder of " + test2);
}

Categories