Understanding and practice with string and replace the use of regular expressions

The following code shows (demo address https://codepen.io/peach_/pen/jONJjRY ):

1. Understand and practice with string and replace the use of regular expressions,

2. Regular expressions learning

3. By Regular Expression thousandths

// string replace understanding and practice 
@ Example Source: HTTPS: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace 
var Re = / (\ W +) \ (\ w +) / S ;
 var STR = "John Smith" ;
 / * 
value of $ 1 $ 2 for each regular expression subexpression (\ w +) and (\ w +), wherein \ s not subexpression but metacharacter 
  / (\ W +) \ S + (\ W +) / 
    $. 1 $ 2 
    
* / 
var newstr1 = str.replace (Re, "$ 2, $. 1" );
 var newstr2 = str.replace (Re, "$ 2 $. 1" ); 



the console.log (newstr1); // Smith, John 
console.log (newstr2); // SmithJohn 

console.log (str.replace (Re, "$ 1 666")); //666 john 

the console.log (str.replace (Re, "$ & AAA")); // John Smith AAA 

name = ' "A", "B"' ; 

the console.log (name.replace ( / "/ G, '. 1')); //   1A1, 1b1 
               // regular matching means "is not followed by the beginning of" a plurality of matching and then " 
the console.log (name.replace (/" ([^ "] *)" / G, " '$. 1'")); //   'a', 'B' 


// in this example, we will first letter of all the words in the string are converted to uppercase: 
// Example source: https: / /www.w3school.com.cn/jsref/jsref_replace.asp 
var NAME2 = 'AAA BBB CCC' ;
 / * 
/ \ B \ W + \ B / G 
regular expression parsing      
\ b matches a word boundary, refers to words and position between the spaces. For example, 'er \ b 'matches "never" in the' er ', but does not match the "verb" in the' er '.
. \ w matching letters, numbers, equivalent to underscore '[A-Za-z0-9_] '. 

* / 

Var your name2.replace = (/ \ b \ + w \ b / g, Function (Word) { 
  the console.log (Word, 'here');   // outputs BBB CCC AAA 
  return word.substring (0,1) .toUpperCase () word.substring + (. 1 ); 
}); 


Console .log (UW, 'are converted to upper case first letter'); // Aaa of Bbb Ccc for 

/ * 
// example source: https: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects / String / replace 
the Fahrenheit to Celsius conversion section of reciprocity 
following example demonstrates how to convert Fahrenheit to Celsius equivalent. Fahrenheit plus a number with a "F" is represented, this function will return plus "C" to indicate a number of degrees Celsius. For example, if the input is 212F, this function will return 100C. If the digital input is 0F, this method will return "-17.77777777777778C" 

Regular expressions test to check whether any of the numbers in F ending. Fahrenheit into the function by the second parameter p1. This function is based Fahrenheit f2c to function as a string provided Celsius. Then f2c, () Returns Celsius. This function is the Perl similar flag s /// e

 * / 

// Parameter Description replace the second set forth as a function of 
/ * 

specify a function as a parameter section 
you can specify a function as the second parameter. In this case, when a match is performed, the function executes. The return value as a replacement string. (Note: the above-mentioned parameters specific replacement can not be used here.) Also note that if the first parameter is a regular expression, and it is a global matching mode, then this method will be called multiple times, each second match will be called. 

The following are the function parameters: 

the variable values representative 
sub-string matches the match. (Corresponds to the above & $.) 
P1, P2, ...     
if the first parameter replace () method is a RegExp object, the n-th string matching brackets represents. (Corresponding to a $ 1 above, $ 2, etc.) For example, if a / (\ a +) (\ b +) / this to match, p1 is matched \ a +, p2 is matched \ b +. 

offset     
matches the offset of the substring in the original string. (For example, if the original string is 'abcd', the matched substring is 'bc', then this parameter will be. 1) 

String matched the original string. 
NamedCaptureGroup named capturing group matched objects 

* / 



function f2c, (X) {
   / * 
  The first parameter is a prerequisite replace regular expression 
  replace if the second parameter is a function of anonymous
    The first parameter is a character string to match 
    the second parameter is followed by a regular expression matching character sub 
    ....      
   The second parameter is the reciprocal of the matched substring in the original string offset is 
   the reciprocal of the first generation of the original string being matched. 
  * / 
  Function Convert (STR, P1, offset, S) { 
    the console.log ( 'here', STR, P1, offset, S); // 212F 212F 212 where 0 is the 
    return ((p1-32). 5 * / . 9) + "C";
   }
   var S = string (X);   // into a string, and toString () distinction see https://www.cnblogs.com/leeke98/p/9754859.html 
  // (:? pattern) matches the pattern but do not get matching results, that this is a non-access match, not stored for later use. This use of "or" character (|) to combine the various parts of a model is useful. For example, 'industr (:? Y | ies) is a ratio of' industry | more brief expressions industries'. 
       // match a word boundary symbol. Or zero or more digits in front of the tenders is matched zero or more times followed by the F-word boundary
   returnvar Test = /(\d+(?:\.\d*)?)F\b/ G;   

  s.replace does (Test, Convert); 
} 
f2c, ( '212F' )
 // -------- ------- 
// above analysis function function f2c (X) {
   / * 
  the first parameter is a prerequisite replace regular expression 
  replace the second parameter is an anonymous function if 
    the first parameter is matched to string 
    the second parameter is a sub-character is a regular expression matching * / function Convert (STR, P1, P2, offset, S) { 
    the console.log ( 'analysis here', str, p1, p2, offset , S); // 212Fhhh HHH 0 212 212Fhhh return ((p1-32) * 5/9) + "C";
   }
   var S = String (X);   //



  
  
    Into a string, and toString () distinction see https://www.cnblogs.com/leeke98/p/9754859.html 
  //Test /(\d+(?:\.\d*)?)F\b/g = var;   
  var Test = /(\d+(?:\.\d*)?)F(\w+)\b/ G; 

  return s.replace does (Test, Convert); 
} 


f2c ( '212Fhhh' ) 

// regular learning address: https: //github.com/ziishaned/learn-regex/blob/master/translations/README-cn.md 
// practice online address (can be copied into the following regular view match effect): https://regex101.com/r/IDDARt/1 
// positive first assertion practice thousandth 
let divide = / \ B (= (? \ {D}. 3) + \ B) / G;
 / * 
\ non-matching character boundary B 
\ B matches the character boundary 
matches a boundary of the non-character, and thereafter the presence of one or more subsequent 3 digits have to have a character boundary, i.e., the rightmost string 
Example: '123' \ B refers between 2 and 3, between 1 and 2   
        \ B 1 refers to the left and right of the 3 
* / 
the let strDivide = '123456789' ;
// console.log(divide.exec(strDivide));
console.log(strDivide.replace(divide, ',') ); //123,456,789

The code above: The last to achieve thousandths of a regular match results (see the results: there are code comments link address):

 

Guess you like

Origin www.cnblogs.com/taohuaya/p/11595118.html
Recommended