1

I have a string str defined as :

const str = `
 @get body {
     Anything here.....
     Any no. of lines

     Or, empty lines as above
 }
`

I made a function get() which, by name gets the element, the @get is marking , and yes the content between the { & }. It can be used something like this :

console.log(get(str))
// body
/*  
     Anything here.....
     Any no. of lines

     Or, empty lines as above
*/

So, the source of my function ( I don't have much experience to use match(), so I usually use replace() for getting the value as well as replacing with anything if and only if I require, so please feel free to edit my code ) :

const get = (val) => {
  val.replace(/@get(.*?)\{([\S\s]*?)\}/gm, (_, a, b) => {
    console.log(a)
    console.log(b)
   }
}

So, now my question is what if I remove the brackets i.e { & } ? Assumed syntax :

const str = `
 @get body 
    Anything here.....
    Any no. of lines

    Or, empty lines as above
 Don't catch me !
`
console.log(get(str))

Now, the output remains same. How how can I make my get() function space sensitive. You can see that the sentence "Don't catch me !", have the same no. of spaces in front of it as of the @get, therefore it is parsed as an external content and is not stated as the content of the @get body block, hence not displayed. So, I am thinking how to do it ? Is it possible in javascript ?

5
  • Are you building a python interpreter? Commented Jan 20, 2020 at 15:41
  • Yeh, it looks alike python, syntax but the answer is no ! I am trying to figure out how simple a syntax can be made in javascript. Commented Jan 20, 2020 at 15:43
  • Why not just read line by line with a bit more code and less regex? Commented Jan 20, 2020 at 15:50
  • Please add an example Commented Jan 20, 2020 at 15:51
  • I have an idea. We can find the no. of spaces between the initial position of a line and the first word i.e @get here, and go on matching it until the same value is found. Afterwards the line with disimilar space counts will be wrapped into { and }, and then it can be easily understood by get(). Is this a good one ? Commented Jan 20, 2020 at 15:56

2 Answers 2

1

Here is how you could do it with a regex. I would add additional logic to remove the leading spaces from the found content:

const str = `
 @get body
    Anything here.....
    Any no. of lines

    Or, empty lines as above
 Don't catch me !
`;

function get(str) {
    let [,,name, content] = (str.match(/^([ ]*)@get\s*(.*\S).*((?:[\r\n]+^\1[ ].*)*)/m) || []);
    content = content.trim().replace(/^[ ]+/gm, ""); // remove leading spaces from all lines
    return [name, content];
}

console.log(get(str));

This requires that the content will consist of lines that have at least one more leading space than the @get line (or are empty).

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

Comments

0

A bit too late on the scene.
But here's a similar function that uses match.

 const str = `
 @get body 
    Anything here.....
    Any no. of lines

    Or, empty lines as above
 Do not catch me
 `
 
 const get = (str) => {
     let arr = str.match(/@get\s+(\S+)\s*[\r\n]+(\s+)(.*[\r\n]+(?:\2.*[\r\n]+|\s*[\r\n]+)*)/);
     let res = { 
 	     title: arr[1], 
 	     indentation: arr[2],
 	     content: arr[3]
     };   
    console.log(res.title, res.content);
 };
 
get(str);

Comments

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.