Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This paper also participates inProject DigginTo win the creative gift package and challenge the creative incentive money

preface

Hello, everyone. Earlier we shared several Symbol built-in properties for object manipulation. Examples include Symbol. HasInstance for instance detection, Symbol. ToPrimitive for object type conversion, and Symbol. ToStringTag for data type detection. These are all related to object manipulation. Next, we share a few Symbol built-in properties related to string manipulation: symbol.match, symbol.replace, symbol.search, and symbol.split.

Symbol.match

String.match() is a function you’ve all used at one point or another, and when we use it, we usually use it to match a String or a regular expression. That is, we are using a match function that normally passes a string or a regular expression as an argument. If you have noticed that when you hold down CTRL and click on the match function with the mouse, you will jump to the definition of the match function, where the source code is like this

/**
  * Matches a string or an object that supports begin matched against, and returns an array
  * containing the results of that search, or null if no matches are foung.
  * @param matcher An object that supports begin matched against.
  */
match(matcher: { [Symbol.match](string: string): RegExpMatchArray | null; }): RegExpMatchArray | null;
Copy the code
  • Matches a string or object, returns an array containing the query results if a match is found, otherwise returns NULL.
  • [Symbol. Match] (string: string) returns an array or null.
  • In other words, it can take an object as well as a string or regular expression. For strings and regular expressions, the internal matching logic is invisible and cannot be modified. The Symbol. Match property opens up the interface for us to define our own internal matching logic. So when we pass the match function an object as an argument and the [symbol.match] (string: string) function exists on the object, the return value of the function will be the result of the match. Look at the following chestnut:
let obj = {
	[Symbol.match](str){
		return str// Customize the matching logic}}'hello'.match(obj);//hello
'world'.match(obj);//world
Copy the code

In the above code, we just return the string without doing anything, so the result is always a match. So we can do some of our own logic here in return STR if the business needs it.

Here we have a simple demonstration of how to use Symbol. Match custom matching logic, normal development needs to be handled according to their own business needs. So much for symbol. match.

Symbol.replace

Symbol. Replace is also a string manipulation property, which, as we might guess from its name, is related to the string replacement function replace. Also take a look at the source definition of replace:

/**
  * Replaces text in a string, using an object that supports replacement within a stirng.
  * @param searchValue A object can search for and replace matches within a string.
  * @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
  */
replace(searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue:string): string;
Copy the code

Symbol. Replace, like Symbol. Match, is an open interface that allows us to implement custom replace logic.

let obj = {
	[Symbol.replace](searchValue, replaceValue){
		let newStr = ' ';
		searchValue.split(' ').forEach(item= >{
			newStr += item[0];
		})
		returnnewStr; }}'hello world and Javascript'.replace(obj, ' ');//hwaJ
Copy the code

In the code above we implement a logic that takes the first letter of each word and forms a new string. As with the match method, the string before the dot is the first argument passed to match or replace. Master this small skill is not stem a matter to come again convenient a lot, want to do what do.

Symbol.search

Next up is the search function in strings, which looks for a substring in a given string and returns the index of the first matched substring. Let’s take a look at the source code definition

/**
  * Finds the first substring match in a regular expression in search.
  * @param searcher An object which supports searching within a string.
  */
search(searcher: { [Symbol.search](string: string):number; }): number;
Copy the code

Symbol. Search: Symbol. Search: Symbol. Search: Symbol. Search: Symbol.

let obj = {
	[Symbol.search](str){
		return str.split(' '); }}'hello world and hello javascript'.search(obj);//['hello','world','and','hello','javascript']
Copy the code

The above code implements a simple example of returning an array with a string separated by a space. Of course, this is just to show the use of symbol.search. The actual logic is also written according to your business needs.

Symbol.split

One last property for string manipulation. I believe you have seen the first three, the basic routine has been clear, nothing more than the needle String split function for us to open a small focus.

/**
  * Split a string into substrings using the specified separator and return them as an array.
  * @param spliter An object that can split a string.
  * @param limit A value used to limit the number of elements returned in the array.
  */
split(spliter: { [Symbol.split](string: string, limit? : number): string[]; }, limit? : number): string[];Copy the code

Its usage is the same as the previous 3, as for the internal business logic can be defined according to the situation, there is no case to show.

conclusion

There are four new things you can do with strings. Interested partners to have a try, today’s share is over.

Like small partners welcome to praise the message plus attention oh!