Home > Net >  Regex finding entire line from paragraph
Regex finding entire line from paragraph

Time:07-15

I need to find the actual line from the paragraph, the paragraph drawing by the markdown editor you can add a checkbox, radio, textbox, and paragraph through the editor.

The actual str is something like this,

  this is paragraph line1

  ?[question_2]{"category":[]}[who are you]=[] {1,2}
  [] OPTION 1
  [] OPTION 2
  [] OPTION 3

  this is paragraph line3

  ?[question_3]{"category":[]}[picode]=_ [PLACEHOLDER_TEXT]

  ?[question_1]{"category":[]}[sex]=() {1}
  () male 
  () femele 


  this is paragraph line3

Do all question types have to start with ?[sometext], so I can use this regex

radio -> [\?] ?[([0-9a-z_].?)]({(?:[^{}]|?)})? ?[?(.?)]? ?[=] ?() ?({[0-9,] })?([^]?)(\n\n|^\n)

checkbox -> [\?] ?[([0-9a-z_].?)]({(?:[^{}]|?)})? ?[?(.?)]? ?[=] ?[] ?({[0-9,] })?([^]?)(\n\n|^\n),

similar to all inputs, my question how can i get the paragraph line text (those are not start with ?[] which may have small/caps/digits include)

CodePudding user response:

You can do it like this:

^(?!(\?)|(\[)). (\n|$)

^ will get you the start of the line, (?! will look ahead for the ? or [ characters, . will match the rest of the characters until \n (Line break) or $ (end of file).

regex101 demo

CodePudding user response:

  • some OS and browsers use different line ends.(not just \n)
  • ^ does not tell the start of the line. it tells the start of entire string.

stay away from regex and just use plain .split(). here's a good example that I've found on SO.

https://stackoverflow.com/questions/21895233/how-to-split-string-with-newline-n-in-node
function splitLines(t) { return t.split(/\r\n|\r|\n/); }

const lines = splitLines(yourString).forEach(line => radioOrCheckBoxRegex(line));

Sorry to deviate from regex but I advise you not to implement markdown by scratch. You'll have endless extreme cases. Because,

  • if it acceps a user input, it requires some degree of sanitation
  • users expectation is always different from your expectation.
  • Related