0

I would like to skip the comments using javascript. I can only get the reverse, meaning not skipping the comments. The following syntax:

#GSM settings\r\n   <----- Skip these
Apn internet\r\n
Pass \r\n
User \r\n
Pin \r\n
Dial *99#\r\n
KeepAlive 0\r\n
#ETH TEST\r\n                  <---- skip
Mac 00:60:37:12:34:56\r\n
EthIpv4 192.168.0.10\r\n
Subnet 255.255.255.0\r\n
Gwip 192.168.0.1\r\n
Autoip 0\r\n
4
  • 1
    Where is the JavaScript? Is that a string? Commented Jan 23, 2013 at 8:03
  • You can replace comments with an empty string, thus removing them, like so: string.replace(/#[^\n]*/g, '') or equivalent. Commented Jan 23, 2013 at 8:07
  • Skip when doing what? Show us your code, please Commented Jan 23, 2013 at 8:14
  • with this I grab the comments:/#.+\s*\r\n/g Commented Jan 23, 2013 at 8:17

1 Answer 1

1

I am not sure I understood 100%, if you want to match rows that are not starting with a "#", you can do it like this:

/^(?!#).*/

This matches only if after the start of the string there is not "#" directly following.

See it here at Regexr

(?!#) is a negative lookahead assertion, that fails if there is a "#" following this position. Since it is after the ^ anchor, the whole expression will fail, if the first character is a "#".

If there can be whitespaces before the "#", you can add them like this:

/^(?!\s*#).*/

See it here on Regexr

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

7 Comments

perhaps for safety's sake include whitespaces in front of the hash or omit the ^.
Actually, the "not" part can be done outside with ! operator. No need for a look-around or matching the rest of the string.
how does the multiline /m affect the output?
@nikolas /^(?!\s*#).*\r\n/gm is the complicated version of stema's one without m parameter. If you specify this param, you need to stop at the linebreak, without you don't.
@nhahtdh mind to provide a regex for that? I never heard of a negative operator other than negative lookarounds or negative character classes.
|

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.