admin管理员组

文章数量:1400115

i got the string like this :

string1 = "Feb 2010 " 
string2 =  "On Going"
string3 =  "may 1990 "
string4 = "jun 1990 "
string5 =  "On Going "

i am trying to write one funcation like this :

function getyear(val)
{
    return val;
}

so if i pass string1 , it should give me "2010", and if pass string2 ,it should be give me "Now",

how it possible using jquery? or java script?

i got the string like this :

string1 = "Feb 2010 " 
string2 =  "On Going"
string3 =  "may 1990 "
string4 = "jun 1990 "
string5 =  "On Going "

i am trying to write one funcation like this :

function getyear(val)
{
    return val;
}

so if i pass string1 , it should give me "2010", and if pass string2 ,it should be give me "Now",

how it possible using jquery? or java script?

Share Improve this question edited Oct 22, 2014 at 18:14 delm ric asked Oct 22, 2014 at 18:04 delm ricdelm ric 536 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

Use a regular expression such as:

/\d{4}/.test(val) ? val.replace(/^[^\d]*(\d{4}).*$/, '$1') : 'Now';

var string = [ "Feb 2010 ", "On Going", "may 1990 ", "jun 1990 ", "On Going "];

function getyear(val)
{
    return /\d{4}/.test(val) ? val.replace(/^[^\d]*(\d{4}).*$/, '$1') : 'Now';
}

$.each( string, function(i,v) {
  $('.out').append( (i+1) + ':\t' + getyear( v ) + '\n');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre class="out"></pre>

Maybe you could do something like this:

function getyear(val){
    var split = val.split(" ");
    for (i = 0; i < split.length; i++){
        if(parseInt(split[i], 10)) return split[i];
    }
    return val;
}

This will return the first int found, and if it can't find one it will return the passed value. That way it returns 'on going' when there is no year, no matter where in the string. You can also pass 'The year I want is 2010' and it should return 2010.

var string = "hello 2014";
var l = string.substr(string.length - 4);
if(l.charAt(l.length - 4) == '1' ||l.charAt(l.length - 4) == '2')
  alert(l.slice(-4));
else
  alert("Now");
  

This should work. using reg exp

function myFunction( val) {
     // val = "year 2014";
    var patt1 = /[0-9]{4}/g;
    
   if ( str.match(patt1) )
        return ( str.match(patt1) );
   else
        return val;
    
}

You can use:

string1 = "Feb 2010 "
string2 = "On Going"
string3 = "may 1990 "
string4 = "jun 1990 "
string5 = "On Going "

function getyear(val) {
    var newVal = "Now";
    return val == "On Going" ? newVal : val.substr(val.indexOf(' ') + 1);
}

console.log(getyear(string1)); //gives 2010
console.log(getyear(string2)); //gives Now

Note that if there is no space will return full string

I'd suggest:

function getYear (str) {
    var year = (/\b\d{4}\b/).exec(str);
  return year === null ? 'Now' : year[0];
}

function getYear (str) {
    var year = (/\b\d{4}\b/).exec(str);
  return year === null ? 'Now' : year[0];
}

var specimins = ["Feb 2010 ","On Going","may 1990 ", "jun 1990 ","On Going "];

specimins.forEach(function (string, index) {
  console.log(index + ': ' + getYear(string));
});

References:

  • Array.prototype.forEach().
  • JavaScript Conditional ('Ternary') operator.
  • JavaScript Regular Expressions ('RegExp').
  • RegExp.exec().

本文标签: how to get quotyearquot from string values in jquery or javascriptStack Overflow