0

Let's say that I want to write a website that could dynamically create tests on a diversity of subjects. The summery of these subjects are saved in a database, and the important parts of the text are between two tags, which I eventually want to check. I want to accomplish it by putting tags between certain words or sentences of a paragraph like this:

Summary in database:

Aristotle lived from [BEGIN] 384 to 322 [/END] was a Greek philosopher and scientist born in the Macedonian city of Stagira, Chalkidice, on the northern periphery of [BEGIN]Classical Greece[/END].

What I want is to read and retrieve the text between the two tags using Javascript which could be in any format, in this example I used [begin] and [/end] but any format will do, like:

  • [begin] text [end]
  • ### text ###
  • ( text )

Is there any way to accomplish this by using Javascript or potentially JQuery?

5
  • 1
    Can't you use HTML? Like <span>384 to 322</span> (He was born after he died, if you don't add BC). Commented Jul 6, 2015 at 23:33
  • Just be careful. If you are retrieving the answers via Javascript, people can easily cheat by looking at the data returned by the server. Instead, the server side language should remove the bits between the tags and handle checking if the user entered the right text. Commented Jul 6, 2015 at 23:35
  • @blex Uhmm I guess I could actually, just a brainfart I guess.. Commented Jul 6, 2015 at 23:36
  • @GregL Thanks. It's more for personal usage though, I don't it would be a huge problem :). Commented Jul 6, 2015 at 23:37
  • You can parse the text if you like, not sure there's a library to assist with parsing random markup. If the intention is to generate HTML, why not just do it on the server? Commented Jul 6, 2015 at 23:42

1 Answer 1

2

Why not just use a simple regex if it's for personal usage?

/\[BEGIN\](.*?)\[\/END\]/g

http://jsfiddle.net/DerekL/fbdy9c20/

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

1 Comment

Great! This is exactly what I needed, even better than using HTML tags. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.