pattern-grab
TypeScript icon, indicating that this package has built-in type declarations

1.0.1Β β€’Β PublicΒ β€’Β Published

pattern-grab

πŸ€›πŸ» Regular Expression Data Grabber

Github Workflow GitHub License Jest Coverage Gzip Size TypeScript NPM Version jsDelivr AMA

Pattern-grab simplifies the process of parsing string patterns using regular expressions.


πŸ“¦ Usage (ES5+)

npm i pattern-grab
import patternGrab from 'pattern-grab'

// Data
const regex = /<[^>]*>/gm
const string = `<span>Yup This is a <b>Test</b> Yea <img src="/blabla.png" /> Its Ok?</span>`

// Pattern Grab
const { data, positions } = patternGrab({ regex, string })


// The HTML tag strings are grabbed.
data === [
  "<span>",
  "Yup This is a ",
  "<b>",
  "Test",
  "</b>",
  " Yea ",
  '<img src="/blabla.png" />',
  " Its Ok?",
  "</span>",
];

// Actually matched elements position are grabbed.
positions === [0, 2, 4, 6, 8]

// It is easy to handle because it is placed with other strings.
data.forEach((element, index) => {
  if(positions.includes(index)){
    // HTML Tag
  } else {
    // Plain text
  }
})

πŸ“¦ Usage (CDN)

<script src="https://cdn.jsdelivr.net/npm/pattern-grab/export/pattern-grab.js"></script>
var patternGrab = window.patternGrab

// Pattern Grab
var grab = patternGrab({
  regex: /<[^>]*>/gm,
  string:  `<span>Yup This is a <b>Test</b> Yea <img src="/blabla.png" /> Its Ok?</span>`
})

// The HTML tag strings are grabbed.
grab.data == [
  "<span>",
  "Yup This is a ",
  "<b>",
  "Test",
  "</b>",
  " Yea ",
  '<img src="/blabla.png" />',
  " Its Ok?",
  "</span>",
];

// Actually matched elements position are grabbed.
grab.positions == [0, 2, 4, 6, 8]

// It is easy to handle because it is placed with other strings.
for(index in grab.data){
  var element = grab.data[index]
  if(grab.positions.indexOf(index) != -1){
    // HTML Tag
  } else {
    // Plain text
  }
}

πŸ’‘ License

MIT Licensed.

Package Sidebar

Install

npm i pattern-grab

Weekly Downloads

6

Version

1.0.1

License

MIT

Unpacked Size

24.2 kB

Total Files

19

Last publish

Collaborators

  • hm-lee