JavaScript

UI, API, Regular Expression, .exec, .test, i, g, (), $, \, \s

Naranjito 2021. 1. 7. 12:50
  • UI : User Interface, it informs the user to the status of the system. It exists between the system and the user, it conveys input devices such as what people thinking about, output devices such as result of computation. 
  • API :  Application Programming Interface. It functionates such as function or method of python. A web browser is created using the API provided by the operating system such as alert box, box position, bar so on. The browser is a base platform for us and we create the application system using  the API provided that platform. 
  • Regular Expression : A sequence of characters that define a search pattern. If I want to extract only url or specific string among  long phrase, use it. 
  • COM : Component Object Model, develope the program using with various component objects. For example, to create the object created by other language in Python, COM can be used.

 

- Create the pattern

var pattern = /a/; //a between two slashes is a target looking for
var pattern = new RegExp('a'); //create an object of regulation expression and looking for a string

 

- Extract the Regular Expression

Extracting the necessaty information from long phrase or not.

.exec : A method which search the pattern I looking for from the subject, and then if there is target in subject, return as array.

> var pattern = /a/; //'a' is a target looking for
< undefined

> pattern.exec('abcde'); // is there 'a' in 'abcde' with only one text after 'a'?
< ["a"] (1) //yes, here is 'a'

> var pattern = /a./; //is there any starts with 'a'?
< undefined

> pattern.exec('abcde'); //yes, here is 'ab'
< ["ab"] (1)

 

- Test the Regular Expression

Test whether url or specific string is there in long phrase or not.

.test : if pattern I am looking for exists in the subject, returns boolean.

> var pattern = /a/; //i wanna search 'a'
< undefined

> pattern.test('abcde'); //is there 'a' here?
< true //yes here it is

 

- Replace the target to other information

> var pattern = /a/;
< undefined

> str.replace(pattern, 'A'); //replace 'a' to 'A'
< "Abcdef"

 

i : undistinguishing uppercase and lowercase

> var xi=/a/;
< undefined

> "Abced".match(xi); //is there a?
< null

> var oi = /a/i; //find any case of a
< undefined

> "Abced".match(oi); //is there any a or A?
< ["A"] (1) //yes, here is A

 

g : Global, give me all the pattern contained in the information

>  var xg = /a/;
< undefined

> "abcdea".match(xg); //is there a? 
< ["a"] (1) //it returns only first a

> var og = /a/g;
< undefined

> "abcdea".match(og);
< ["a", "a"] (2) //it returns all a

> var ig = /a/ig; //give me any a regarless uppercase or lowercase
< undefined

> "AabcdAa".match(ig);
< ["A", "a", "A", "a"] (4)

 

() : group

$ : when it call the group

\ : character(A~Z, a~z, 0~9)

\s : space, white space

> var pattern = /(\w+)\s(\w+)/; // w+ means more than one word
< undefined

> var str = "coding everybody";
< undefined

> var result = str.replace(pattern, "$2, $1");//replace str the character of pattern to $2(second group), $1(first group)
< undefined

> console.log(result);
< undefined