0

i want to check particular character occurrence in a string java script,
following is what i need

  • if the string having only one dot(.) between the string i have to alert it is a object
    eg: var text = 'ABC.DEF';
  • if the string having open and closing brackets () at the very end of the string i have to alert it is a function
    eg: var text = 'ABC()';

i tried this

if(text .indexOf('()') === -1) { alert("not function"); } 

but how i can check whether brackets are in very end.

4
  • That is just JavaScript, not jQuery. Commented Feb 25, 2014 at 10:53
  • 3
    What if I have foo.bar()? Or foo.position().top? Commented Feb 25, 2014 at 10:54
  • @JosephtheDreamer how? some demo?? Commented Feb 25, 2014 at 10:57
  • Let me iterate that again: What if the string has both . and ()? Where does it belong? Commented Feb 25, 2014 at 11:03

7 Answers 7

3

You an use RegEx:

var one = "ABC.DEF"; var two = "ABC()"; var three = "()blank"; function check(string){ // Matches: ABC.DEF and Does not match: ABC. or .DEF if(/\w+\.\w+/.test(string)) console.log("it is a function"); // \(\) interpreted as (). Matches : ABC() ; Does not match: ()ABC or ABC()ABC else if(/\w+\(\)$/.test(string)) console.log("it's an object"); // Not found else console.log("something else") } check(one); // it is a function check(two); // it's an object check(three); // something else 

The $ checks if the match(()) is at the end of the line.
The \w+ is count one or more occurrences of "A-Za-z0-9_".

JSBin

Sign up to request clarification or add additional context in comments.

Comments

1

The regular expression ^\w+\.\w+$ will match a string consisting of:

  • the start of the string;
  • a sequence of "word" characters;
  • followed by a full stop;
  • then another sequence of "word" characters; and
  • finally, the end of the string.

Similarly, the regular expression ^\w+\(\)$ will match a string consisting of:

  • the start of a string;
  • a sequence of "word" characters;
  • followed by open and closing round brackets; and
  • finally, the end of the string.

You can wrap it in a function like this:

function check( text_to_match ){ if(text_to_match.match(/^\w+\.\w+/)) console.log("it is an object "); else if(text_to_match.match(/^\w+\(\)$/)) console.log("it is an function"); } 

Comments

1

try

 var a = "abc()"; if (a[a.length-1] == ')' && a[a.length - 2] == '(') { alert("function"); } 

Comments

1

http://jsfiddle.net/h9V2z/2/

String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; if("ABC()".indexOf("()") > -1) { alert("it is a function"); } if("ABC.DEF".indexOf(".") > -1) { alert("it is an object"); } if("ABC()".endsWith("()")=== true) { alert("ends with ()"); } if("ABC()".endsWith("()whatever")=== true) { alert("ends with ()"); } 

Comments

0

indexOf returns the position of the string in the other string. If not found, it will return -1:

var s = "foo"; alert(s.indexOf("oo") != -1); 

2 Comments

i want to know whether that character is in very end of the string or not
Indexof return the position of your substring. Just check if for example "(" return s.length - 1 ;)
0

Try this:

 if(text.substring(2) == "()") { alert("it is function"); } 

Comments

0

String.prototype.slice lets you use negative numbers to tackle the other end of the String.

'foobar'.slice(-2); // "ar" 

so

if (text.slice(-2) === '()') { // last two digits were "()" } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.