tags:

views:

50

answers:

2

Hi

I have a variable. I want to find out if it contains any alphabets if so then append '(single quote) before and after to that variable. if there are no alphabets then no need to append.

var mysample = $('p').text();

find out if this var "mysample" contains alphabets are not if so then append ' before and after that value.

I think i need to use REGEXP but i am not sure how to do that. Can some one point me out what to do.

I am looking for a jquery formate solution.

Thanks Kumar

A: 

Have a look at Regular Expressions in JavaScript.

Using something like [a-zA-Z].

astander
+1  A: 

Assuming you mean wrap a single quote around the text.. you can create a custom jquery filter.

jQuery.expr[':'].containsAlphabets = function(element) {
    return /[a-zA-Z]+/.test($(element).text());
}

And then combine it with any jQuery selector. This code below will wrap the text of all paragraphs containing alphabets with single quotes.

$("p:containsAlphabets").each(function() {
    $(this).prepend("'").append("'");
});
Anurag