how to check "contains" using jquery [duplicate] - javascript

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 9 years ago.
I tried of checking contains option using jquery for the birthday but its getting exception
var _dob = "4/10";
// this line doesn't work
var _adob = _dob.contains('/') ? _dob.Split('/') : _dob.Split('-');
$('#Month').val(_adob[0]);
$('#Day').val(_adob[1]);
but i can't able to split.. its resulting in error on getting _adob itself

Try this:
var _dob = "4/10";
var _adob;
if (_dob.indexOf("/") >-1) {
_adob = _dob.split("/");
} else {
_adob - _dob.split("-");
}

Direct Answer
indexOf(something)>-1
var _dob = "4/10";
var _adob = _dob.indexOf('/')>-1 ? _dob.split('/') : _dob.split('-');
$('#Month').val(_adob[0]);
$('#Day').val(_adob[1]);
Indirectly
You really don't need to check that the string contains that... Using a regular expression, you can split on a -, /, or . by building a character set:
var _dob = '4.10';
var _aodb = _dob.split(new RegExp('[-/.]'));

Related

Expression for getting text between 1 or 2 sets of parens? [Javascript] [duplicate]

This question already has answers here:
How to match string within parentheses (nested) in Java?
(2 answers)
Closed 4 years ago.
I have the following:
POLYGON((7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531))
I can also have the following:
POLYGON(7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531)
Noting the difference in parens. I only always wants the data inside the innermost set of parents so I can split on the commas.
I had something like this let coordFinder = /\(([^)]+)\)/g; but it's not getting me both cases.
let coordFinder = /.*\((.*?)\)/;
var test1 = '((abc(123,456)))';
var test2 = '(abc)';
console.log('result in test1: ' + test1.match(coordFinder)[1]);
console.log('result in test2: ' + test2.match(coordFinder)[1]);
You could do one of the following:
\([^()]*\)
Or, getting down to the digits and dots and commas,
\([.,0-9 ]*\)
UPDATE: added snippet (works as expected) => matches both versions with one or two sets of parameters
var poly2 = "POLYGON((7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531))",
poly1 = "POLYGON(7.593955993652344 33.70124816894531,3.1060409545898438 24.7247314453125,8.64349365234375 22.052650451660156,14.989128112792969 26.966629028320312,7.593955993652344 33.70124816894531)";
console.log(poly1.match(/\([.,0-9 ]*\)/));
console.log(poly2.match(/\([.,0-9 ]*\)/));

How can I adapt this javascript to return a postcode less the last 2 characters? [duplicate]

This question already has answers here:
How do I chop/slice/trim off last character in string using Javascript?
(25 answers)
Closed 7 years ago.
function() {
var address = $("#postcode").val();
var postcode = address.split(' ');
postcode = "Postcode:"+postcode[(postcode.length-2)];
return postcode;
}
This js pulls a postcode value from an online form when the user runs a query. I need to know how I get it to deliver the postcode less the last 2 characters. for example, SP10 2RB needs to return SP102.
Use substring() or substr() or slice().
You have to slice the string and return what you want:
return postcode.slice(0, -2);
// example
postcode = "sample";
// output
"samp"
You can use this function:
function postCode(address)
{
var tmpAddr = address.replace(' ','');
tmpAddr = tmpAddr.substr(0, tmpAddr.length-2);
return tmpAddr;
}
alert(postCode('SP10 2RB'));

How to extract URL parameters with jquery [duplicate]

This question already has answers here:
Getting URL hash location, and using it in jQuery
(7 answers)
Closed 8 years ago.
I have somes links like this:
mywebsite.com/tutos.php?name=tuto_name#comments
mywebsite.com/tutos.php?name=tuto_name#download
My question: how to get the text after the #.
thanks.
window.location.hash is a cross browser solution that returns the value (including the hash)
You can remove the hash by doing:
var hash = window.location.hash.substr(1);
I use the following as it not only grabs the hash value (without the hash itself (taking the 2nd part (array[1] of the split)), but also tests the undefined case as well which can cause problems in some cases.
var hashVal = window.location.hash.split("#")[1];
if( hashVal && hashVal != "undefined" ) {
//work it
}
I use the following JS function that will do this:
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)','i').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
}
You can use window.location.hash. It takes with # (ie, #comments). To remove trialing # use .substring(1). Example:
var str = window.location.hash.substring(1);
alert(str);

A function on checking if the string is a url in Javascript [duplicate]

This question already has answers here:
Check if a JavaScript string is a URL
(36 answers)
Closed 9 years ago.
I am writing a function to check if the input string is a url in Javascript. Should I use substring(0,6) and see if starts with "http://"? Or there is a better way to achieve? Cheers.
Something like this should handle the simple cases:
function is_url(url) {
return Boolean(url.match(/^https?:\/\//));
}
You could use a regular expression
/^http:\/\//.test(urlString)
You could use:
if(myvalue.indexOf('https://') == 0 || myvalue.indexOf('http://') == 0)
Depends how detailed you want to get with it. I am sure you can find a regex that would do it on here is you searched around.
With regex:
/^http:/.test("http://example.com/")
If you wanted to check www too: /^(http:|www\.)/.test("http://example.com/")
And to be different:
function matchString(str,matches)
{
if(matches)
{
matchString.toCheck=matches;
}
var matched = [];
for(var i=[0,str.length];i[0]<i[1]; i[0]++)
{
for(var j=[0,matchString.toCheck.length];j[0]<j[1]; j[0]++)
{
if(!matched[j[0]])matched[j[0]]={c:0,i:-1};
if(matchString.toCheck[j[0]][matched[j[0]].c]==str[i[0]])
{
matched[j[0]].c++;
if(matched[j[0]].i==-1)matched[j[0]].i=i[0];
}
else if(matchString.toCheck[j[0]].length!=matched[j[0]].c)matched[j[0]]={c:0,i:-1};
}
}
return matched;
}
var urlVariants = matchString("https://",["http://","https://","www."]);
var isUrl = false;
for(var i=[0,urlVariants.length]; i[0]<i[1]&&!isUrl; i[0]++)
{
isUrl = (urlVariants[i[0]].i==0);//index at the start
}
console.log(isUrl);
I think regex is a better solution:
function isAnUrl(url){
var expression = /[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
if (url.match(regex))
return true;
else return false;
}

How to remove $ from javascript variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove characters from a string
I have a variable p which I would like to remove the $ from. This variable will be a number such as $10.56. How can I do this? I thought it could be done using .replace('$','') but i'm not quite sure how to implement this.
Here is my javascript code:
function myFunction() {
var p = parseFloat(document.getElementById('p_input').value);
var q = parseFloat(document.getElementById('q_input').value);
if (!q){
document.getElementById('t').value = '';
}
else {
var t = q * p;
document.getElementById('t_output').value = t;
}
}
It's pretty simple:
var myString = "$15.62"
console.log(myString.replace('$', ''));
//Logs: "15.62"
Please note that this new value is not actually "saved" to myString, you'll have to assign it to a variable, yourself:
var newString = myString.replace('$', '');
Try this, assuming that the values of p_input and q_input will be the money values:
var p = parseFloat(document.getElementById('p_input').value.replace('$', ''));
var q = parseFloat(document.getElementById('q_input').value.replace('$', ''));

Categories

Resources