Recently I have been looking at data structures and algorithms, trying to sum up my debut ~

Foundation: Create a re

Create a re in two ways:

  • /ab+c/giIs applicable to static regular expressions
  • new RegExp("ab+c","gi"), for dynamic expressions

The gi flag is optional, indicating that global and case-insensitive search is allowed.

Basic: JS how to use the re

JS uses re, nothing more than to call methods, one is the re object of the two methods exec and test methods, one is the string of five methods match, matchAll, replace, search and split methods.

Common estimates are exec, test, match, replace. Note which object methods are used to make sure the calls are accurate.

  • Look at the stringIs there anyThe string that matches is generally usedtestTo return totrue/false, such as/^hello/.test('hello world! ')returntrue
  • Look at the string matching the specific string, general usematchTo return toThe array/null, such as'Maria has a sheep named Yan'.match(/[A-Z]/g)To return to['M','Y']
  • Look at the string matching string details, general useexecTo return toThe array/null, you need the first one directly, but you need all the details, you need to loop. Because exec only returns the details of a matching string
function getMatch(str, reg) {
  let resArray = [];
  let res = reg.exec(str);

  while (res) {
    resArray.push(res);
    res = reg.exec(str);
  }
  return resArray
}
console.log(getMatch('table football, foosball'./foo*/g))

Copy the code

  • The contents of the string need to be replaced, usually with replace, which returns the new string but does not change the original string. Replace The second argument can be a callback function, and the return value is the value of the replacement. Complex cases can be used in combination with functions.
var str = 'Twas the night before Xmas... ';
var newstr = str.replace(/xmas/i.'Christmas');
// Twas the night before Christmas...
console.log(newstr);
Copy the code

Exercise: Search for strings

Add is actually a storage operation. In order to make later search faster, the string length is taken as the key, and strings of the same length are stored in an array. The overall structure is a Map.

Search, without a dot, is just a search for ordinary strings. Now, the tricky part is when you have a dot, if you do it in a non-regular way, you have to figure out where the dot is, and then look at the string. But with the re, it’s very convenient, you just set up the re.

var WordDictionary = function() {
    this.words = {}

};

/ * * *@param {string} word
 * @return {void}* /
WordDictionary.prototype.addWord = function(word) {
    const len = word.length
    // If there is no key, create
    this.words[len] || (this.words[len] = [])
    // Store it in the corresponding key
    this.words[len].push(word);
};

/ * * *@param {string} word
 * @return {boolean}* /
WordDictionary.prototype.search = function(word) {
    const len = word.length
    // The word table corresponding to the key
    const list = this.words[len]
    // No word list, just false
    if(! list)return false
    const hasPoint = word.includes('. ')
    // See if there is a word
    if(! hasPoint)return list.includes(word)
    // Set up the re
    const reg = new RegExp(word)
    // see if there are any matching words
    return list.some(item= >reg.test(item))

};
Copy the code

Exercise: Converting strings to numbers

Theorem: the topic is long, means that the topic is simple, the test is patience ~~

  • According to number one, step one, remove the space
  • According to number two, step two, determine if there’s a plus or minus sign, or if there’s no sign, it’s positive
  • According to rule 3, step 3, stop capturing when you encounter a non-numeric or ending
  • According to step 4, convert the numeric part of the string to a number
  • According to the fifth, the fifth step, whether the transformed number is out of bounds, the maximum and minimum is limited

If you’re using non-regular methods, the first three steps need to be handled individually, but if you’re regular, you can do it all at once.

var myAtoi = function (s) {
  // \s is blank, optional; +- Either one is optional; The following number must have, the main capture is the symbol plus number
  const reg = / ^ \ s {0} ([+ -] [0-9] {0, 1} {1}) /;
  // it is enough to catch the first one, exec is enough
  const match = reg.exec(s);
  // No catch is null, return 0
  if(! match)return 0;
  // If caught, the second item is the desired numeric string, minus 0 is converted to the numeric type
  const number = match[1] - 0;
  const min = -Math.pow(2.31);
  const max = Math.pow(2.31) - 1;
  // boundary value processing
  return number > max ? max : number < min ? min : number;
};
Copy the code

The official video

reference

  • Regular interpretation of MDN
  • The front-end algorithm and data structure of xiuyan