-1

I am trying to parse a formula, and display it on screen. For example I should be able to take <path>T Q, where <path>T cannot change, and Q is a variable. It accepts it,however when printing it on screen again the only thing that will appear is T Q. I want <path>T Q to appear fully.

Other examples of accepted formulae are

(B & A)

~A

~(B&A)

<path>T (B & A)

etc

My code is something like this

  var beginPartBUC      = '^<path>\\(',
  beginPart = '^\(',
  unaryPart         = '(?:~|<path>T)',
  propOrBinaryPart  = '(?:\\w+|\\(.*\\))',
  subwffPart        = unaryPart + '*' + propOrBinaryPart,
  endPart           = '\\)$';

// binary connective regexes

  var conjRegEx = new RegExp(beginPart + '(' + subwffPart + ')&(' + subwffPart + ')' + endPart), // (p&q)
  implRegEx = new RegExp(beginPart + '(' + subwffPart + ')->('  + subwffPart + ')' + endPart), // (p->q)
  equiRegEx = new RegExp(beginPart + '(' + subwffPart + ')<->(' + subwffPart + ')' + endPart); // (p<->q)
 // untilRegEx = new RegExp(beginPartBUC + '(' + subwffPart + ')U('   + subwffPart + ')' + endPart);    //<path>(p U q))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    How are you displaying it? If you're putting it in `.innerHTML`, the angle brackets will be treated as HTML tags. You should put it in `.textContent` so it won't be parsed as HTML. – Barmar Apr 19 '15 at 15:19
  • In SO, put code in backticks so it will be displayed literally, instead of being processed as HTML. – Barmar Apr 19 '15 at 15:20
  • it gets displayed by calling the method, and appending the parsed formula in html to a div currentFormula .html('Current formula:
    ' + wff.ascii() ) where wff.ascii is the formula. Where would the backtick be put?
    – Zainab Uddin Apr 19 '15 at 16:00

1 Answers1

0

As Barmar pointed out, you're writing to html and <path> resembles valid html. You can do this

currentFormula.html('<strong>Current formula:</strong><br>' + wff.ascii.replace(/>/g, "&gt;").replace(/</g, "&lt;"))

As an additional note, backticks are used on StackOverflow like this: `sample code` which produces sample code. This feature is available in comments as well.

Alternatively, in posts (not comments), you can indent each line with a tab or four spaces (easily done by pressing { } in the post editor.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47
  • I see! This is my first time seeing backticks, and in direct HTML i could see how it'd work but It wasn't quite working in my program. This solution worked perfectly, thank you! So what exactly does this replace the > and < with? – Zainab Uddin Apr 19 '15 at 17:46
  • @ZainabUddin `` looks just like HTML. `` doesn't display as text for the same reason ``, ``, and `
    ` do not. So we replace `>` with `>`, the proper entity for representing `>` *on* the page, likewise with `<` and `<`.
    – Regular Jo Apr 19 '15 at 19:22