I am trying to extract a component name from a string so I can parse the rest with JSON and also return the component name.
Let's say I have a string as follows:
namespace/Slider {"showCaptions":true,"circular":false,"autoPlay":true,"showItemNavigators":true,"showItemNavigatorsOnHover":true,"showIndicators":false,"indicatorsPosition":"bottom","showThumbnails":true,"numVisible":10,"numVisible1536":8,"numVisible1280":6,"numVisible1024":4,"numVisible768":3,"numVisible640":2,"changeItemOnIndicatorHover":false,"images":[{"id":1701,"url":"http://localhost:10019/wp-content/uploads/2022/12/horse.jpeg","alt":"horse alt","caption":"Name some name"},{"id":1699,"url":"http://localhost:10019/wp-content/uploads/2022/12/GPJNews_Mongolia_KK_RidingChild_076_web-920x613-1.jpeg","alt":"","caption":"Name GP"},{"id":1238,"url":"http://localhost:10019/wp-content/uploads/2022/08/test-scaled.jpeg","alt":"","caption":""},{"id":458,"url":"http://localhost:10019/wp-content/uploads/2022/09/news-scaled-1.jpeg","alt":"","caption":"Paswan a mother who..."}]}
I want to remove the "namespace/Slider" part from the string and return the rest so I can JSON.parse
it and also want to return "Slider" so the part after namespace as a variable.
CodePudding user response:
Take the substring starting from the first {
character.
let str = 'namespace/Slider {"showCaptions":true,"circular":false,"autoPlay":true,"showItemNavigators":true,"showItemNavigatorsOnHover":true,"showIndicators":false,"indicatorsPosition":"bottom","showThumbnails":true,"numVisible":10,"numVisible1536":8,"numVisible1280":6,"numVisible1024":4,"numVisible768":3,"numVisible640":2,"changeItemOnIndicatorHover":false,"images":[{"id":1701,"url":"http://localhost:10019/wp-content/uploads/2022/12/horse.jpeg","alt":"horse alt","caption":"Name some name"},{"id":1699,"url":"http://localhost:10019/wp-content/uploads/2022/12/GPJNews_Mongolia_KK_RidingChild_076_web-920x613-1.jpeg","alt":"","caption":"Name GP"},{"id":1238,"url":"http://localhost:10019/wp-content/uploads/2022/08/test-scaled.jpeg","alt":"","caption":""},{"id":458,"url":"http://localhost:10019/wp-content/uploads/2022/09/news-scaled-1.jpeg","alt":"","caption":"Paswan a mother who..."}]}';
let idx = str.indexOf('{');
console.log(str.slice(str.indexOf('/') 1, idx));
console.log(JSON.parse(str.slice(idx)));
CodePudding user response:
Trivial - replace non-greedy everything from start to first {
const str = `namespace/Slider {"showCaptions":true,"circular":false,"autoPlay":true,"showItemNavigators":true,"showItemNavigatorsOnHover":true,"showIndicators":false,"indicatorsPosition":"bottom","showThumbnails":true,"numVisible":10,"numVisible1536":8,"numVisible1280":6,"numVisible1024":4,"numVisible768":3,"numVisible640":2,"changeItemOnIndicatorHover":false,"images":[{"id":1701,"url":"http://localhost:10019/wp-content/uploads/2022/12/horse.jpeg","alt":"horse alt","caption":"Name some name"},{"id":1699,"url":"http://localhost:10019/wp-content/uploads/2022/12/GPJNews_Mongolia_KK_RidingChild_076_web-920x613-1.jpeg","alt":"","caption":"Name GP"},{"id":1238,"url":"http://localhost:10019/wp-content/uploads/2022/08/test-scaled.jpeg","alt":"","caption":""},{"id":458,"url":"http://localhost:10019/wp-content/uploads/2022/09/news-scaled-1.jpeg","alt":"","caption":"Paswan a mother who..."}]}`
console.log(JSON.parse(str.replace(/^.*?{/,"{")))