Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-25-2012, 11:35 PM   PM User | #1
anarchos78
New to the CF scene

 
Join Date: Oct 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
anarchos78 is an unknown quantity at this point
Check a string for 1 to infinite repetitions of asterisk (*) using JavaScript regex o

I am completely crap in regex and I need a little help with a task. How do I check a string for a single character repetition (in my case *) using javasctipt regex object. Specifically I need to check a string for 1 to n repetitions of * character. Notice that i need to check a string that contains only asterisks not any other character ex. not validate "tom***" or "**tom" or "*tom*" but "*" or "**" or "**n times". If the above case is true return true. Forgive my "noviceness".

With respect,

Tom

Greece
anarchos78 is offline   Reply With Quote
Old 08-26-2012, 12:10 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
var expr = "**a";
var regex = /[^\*]/g;
if (!expr.length || expr == '') {
    alert('Empty string');
} else if (regex.test(expr)) {
    alert('Not just stars');
} else {
    alert('Just stars.');
}
seems to work
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-26-2012, 12:19 AM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Try ...
Code:
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">
function countStars(str) {
  alert(str+': '+/^\*+$/.test(str));
}
countStars('***')
countStars('*')

countStars('***tom');
countStars('tom**');
countStars('***tom**');
</script>

<style type="text/css">

</style>
</head>
<body>

</body>
</html>

Last edited by jmrker; 08-26-2012 at 12:20 AM.. Reason: Alternate solution submitted too late because I type too slow! :(
jmrker is offline   Reply With Quote
Old 08-26-2012, 02:59 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I've slightly revised my example as I don't need to escape \ the asterisk as it is within a character class, and the global modifier is unnecessary with test():

Code:
var expr = "**";
var regex = /[^*]/;
if (!expr.length || expr == '') {
    alert('Empty string');
} else if (!regex.test(expr)) {
    alert('Just stars.');
}​
The regex.test() expression essentially says "return true as soon as you find a character other than an asterisk, otherwise return false".
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 08-26-2012 at 03:08 AM..
AndrewGSW is offline   Reply With Quote
Old 08-26-2012, 10:49 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<script type = "text/javascript">

var str = "***tom***";
if (/[^\*]/gi.test(str)) {
alert ("String contains characters other than asterisks");
}

var str = "*********";
var len = str.length;
alert ("There are " + len + " asterisks in your string")

</script>

The cheese in the mousetrap is always free.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, regex

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:14 PM.


Advertisement
Log in to turn off these ads.