Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17391

Re: A simple regexp question

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: A simple regexp question
Date 2012-12-01 09:58 +0100
Organization albasani.net
Message-ID <k9cgr3$a43$1@news.albasani.net> (permalink)
References <6759f512-f267-4919-bbb9-8ab3450ecb30@c14g2000vbd.googlegroups.com>

Show all headers | View raw


On 2012-12-01 07:20, justaguy wrote:
> I have a json string that reads like this:
>  ('partname':['Antenna'],'partdescription':['Sensor Systems Dual
> Iridium Antenna;'])
> 
> The tool I'm using to parse json
> JSON.parse(theString)
> failed, complaining about unexpected character.
> 
> So, I wonder if I remove all the occurences of [' and '] would help.

It would not, because those aren't the unexpected characters. JSON
strings are serializations of objects or arrays, and as such they always
start with "{" or "[", respectively. Strings values have to be in double
quotes.

The JSON syntax is very simple; take a look at the diagram on this page:
http://json.org/

The syntactically correct JSON form of your string would be

  {"partname":["Antenna"],"partdescription":["Sensor Systems Dual
  Iridium Antenna;"]}

(after removing the line break artifact).

Also note that your object contains arrays as values for partname and
partdescription. If that's not what you want, use strings instead:

  {"partname":"Antenna","partdescription":"Sensor Systems Dual
  Iridium Antenna;"}

The semicolon after Antenna looks unintentional, too, but I don't know
what you intend to do with the data.

> I attempted
> replace(/['/g,"")
> but it failed.
> 
> What's wrong with the above global replace syntax?

"[" is a special character in regular expressions. If you want to use it
literally, you need to escape it:

  str.replace(/\['/g, "");


- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A simple regexp question justaguy <lichunshen84@gmail.com> - 2012-11-30 22:20 -0800
  Re: A simple regexp question Stefan Weiss <krewecherl@gmail.com> - 2012-12-01 09:58 +0100
    Re: A simple regexp question justaguy <lichunshen84@gmail.com> - 2012-12-01 07:35 -0800
      Re: A simple regexp question justaguy <lichunshen84@gmail.com> - 2012-12-01 07:43 -0800

csiph-web