7 Replies - 1048 Views - Last Post: 10 December 2011 - 02:47 PM

#1 Sho Ke   User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Problem with Regular Expressions

Posted 09 December 2011 - 09:16 AM

Hey everyone, I've recently been dabbling in the art of Regular Expressions, and have decided to make a simple bbcode-to-html converter, and have come across a problem with matching a string using regular expressions. (code below)

Right now, I'm using the regex /^\[[a-zA-Z]\]$/mg , which, according to JavascriptLab,

Quote

Match[es] a string which starts with the character "["
followed by any letter a-z
ending in the character "]"

link

In the code I've made, when I test the regex with the text "[b]", it returns true. However, when I test the regex with the text in a textarea, it returns false- even if the textarea only says "[b]", and nothing else.

Here's the code I'm using:
<html>
<head>
	<title> BBCode Offline Editor </title>
	
	<script type="text/javascript">
	/* 
		Regex to use. Searches for a string that starts with 
		" [ " , followed by any letter a-z, A-Z,
		/^\[[a-zA-Z]\]$/
	*/
	
	function convert() {
		
		var bbcode = document.getElementById("bbcode").value;
		var regex = /^\[[a-zA-Z]\]$/mg;
			alert(regex.test("[b]")); //test return true
			alert(bbcode); //Typing in "[b]" in the textarea results in the alert returning "[b]"
			alert(regex.test(bbcode)); //test returns false when the textarea only has "[b]"
		//document.getElementById("result").innerHTML = bbcode.replace(regex, "<p>");
		document.getElementById("result").innerHTML = bbcode;
		
	}
	</script>
	
	<style type="text/css">
	/* styling later */
	</style>
</head>
<body>
<p>BBCode</p>
<textarea id="bbcode"></textarea>
<p>Result:</p>
<textarea id="result"></textarea>
<p> <input type="button" value="Display" onclick="convert()" /> </p>
</body>
</html>


Any help would be greatly appreciated, as always!
- Sho Ke

This post has been edited by Sho Ke: 10 December 2011 - 10:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Regular Expressions

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Problem with Regular Expressions

Posted 10 December 2011 - 10:22 AM

Hey, remove the extra space within between your textarea tags (line #31, 33)
Was This Post Helpful? 0
  • +
  • -

#3 Sho Ke   User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Problem with Regular Expressions

Posted 10 December 2011 - 11:02 AM

After removing the whitespace, I tried it again but with the same result. Any other suggestions?

Also, after some testing, I found that removing the "g" in the regular expression will make the test in line 18 return true, but only when the value of the textarea is exactly "[b]". If the value is "[b] "(extra space), it returns false.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Problem with Regular Expressions

Posted 10 December 2011 - 11:36 AM

The g won't affect if it finds a match or not and yes a whitespace does matter. Try the value before using it because it works perfectly on my end when I removed the unnecessary whitespaces.
Was This Post Helpful? 0
  • +
  • -

#5 Sho Ke   User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Problem with Regular Expressions

Posted 10 December 2011 - 01:12 PM

Hmm, since adding a space after the "[b ]" makes the regex say false, would this approach be wrong all together?

What I'm trying to do with this regex is find all cases of "[", followed by text, ending in "]" and replace them with something else later on, depending on what the surrounded text is. For example, someone may type out "[b ]test[/b] test test", and the result textarea would print out something like "<b>test</b> test test"(a textarea would just print out the <b>, instead of actually making it bold, but this is just for testing purposes). Could a regex do this? I have a few other ideas, and could start working on those if not.

Thanks
-Sho Ke

edit: added spaces to the [b]'s so it wouldn't make my text bold. Assume hte [b ]'s don't have that space :D

This post has been edited by Sho Ke: 10 December 2011 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Problem with Regular Expressions

Posted 10 December 2011 - 01:56 PM

Are you just trying to learn? Because if not and this is for some project, then TinyMCE has a BBCode plugin that would make your life a whole lot easier.
Was This Post Helpful? 1
  • +
  • -

#7 Sho Ke   User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Problem with Regular Expressions

Posted 10 December 2011 - 01:59 PM

I'm doing this just as a learning experiment. I read somewhere in the Lounge that someone made a bbcode editor, and wanted to see what it would be like. Thanks though.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Problem with Regular Expressions

Posted 10 December 2011 - 02:47 PM

Potentially helpful link
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1