Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript verifies the existence of substrings in string arrays

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how JavaScript verifies whether there are substrings in the string array". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Split string

The split () method in JavaScript splits an String object into an array of substrings using the specified delimiter string, and determines the location of each split with a specified split string. There are two optional parameters (delimiter and optional limit count) to convert the string to a character or substring array, and not setting the delimiter returns the full string in the array. Delimiters can take a single character, a string, or even a regular expression. Here is the code that uses regular expressions to split strings using commas and spaces:

Const title = "4, JavaScript string tricks"; console.log (title.split (/ [\ squarry /] + /)); / / ['4', 'JavaScript',' string tricks'] console.log (title.split (/ [\ squarry /] + /, 2)); / / ['4', 'JavaScript']

Strings split by the split () function can be concatenated simply by join ("").

2. JSON formatting and parsing

JSON is not a JavaScript-only data type and is widely used for front-and back-end data interaction. The JSON.stringify () function is used to convert an object to a string in JSON format. Typically, you can simply take the object as a parameter, as follows:

Const article = {title: "JavaScript string skills", view: 30000, comments: null, content: undefined,}; const strArticle = JSON.stringify (article); console.log (strArticle); / / {"title": "JavaScript string skills", "view": 30000, "comments": null}

As you can see from the above code, the value of undefined is filtered out in stringify, but the value of null is not.

JSON.stringify () accepts two optional arguments, the second of which is a replacer, where you can specify an array of keys to print or clear their functions. The code is as follows:

Console.log (JSON.stringify (article, ["title", "comments"])); / / {"title": "JavaScript string skills", "comments": null} console.log (JSON.stringify (article, [])); / / {}

For a large JSON, passing a long array may affect readability and efficiency. Therefore, you can set the replacement function and return undefined for the key to be skipped, as shown in the following code:

Const result = JSON.stringify (article, (key, value) = > key = = "title"? Undefined: value); console.log (result); / / {"view": 30000, "comments": null}

The third parameter of JSON.stringify () formats JSON by specifying indentation (useful in nested blocks), passing a number to set indentation spacing, or even a string to replace spaces. The code is as follows:

Console.log (JSON.stringify (article, ["title"], "\ t"))

The format of the output is as follows:

{

"title": "JavaScript string skills"

}

There is also a JSON.parse () function that takes a JSON string and converts it to a JavaScript object. It also accepts a reviver function that can intercept object properties and modify property values before returning a value.

Const reviver = (key, value) = > (key = = "view"? 0: value); var jsonString = JSON.stringify (article); var jsonObj = JSON.parse (jsonString, reviver); console.log (jsonObj); / / {title: 'JavaScript string skills', view: 0, comments: null} 3. Multiline strings and embedded expressions

There are three ways to create a string in JavaScript, using single quotation marks, double quotation marks, or backquotes (the top left of the keyboard, press the left key of 1).

Const countries1 = "China"; const countries2 = "China"; const countries3 = `China`

The first two are created in basically the same way, and can be mixed and matched to concatenate or add quoted strings (by using the opposite syntax style), while backquotes can perform gaudy and powerful manipulation on strings.

Backquotes, also known as template literals, are convenient when creating multiline strings and embedded expressions. Here is the code for how to create multiple lines of strings using string interpolation in JavaScript:

Const year = "2021"; const month = 7 Const day = 2 X Const detail = `Today is ${year} ${month} month ${day} day, is a good day! `; console.log (detail)

The result of the output is also wrapped, as follows:

Today is July 2, 2021.

It's a good day!

In addition to the literal amount of the string, any valid expression is allowed in ${}, which can be a function call or expression, or even a nested template.

Tag templates are an advanced form of template literals, which allow you to use a function to parse template literals, where embedded expressions are parameters. The code is as follows:

Const title = "JavaScript string skills"; const view = 30000 string2 Const detail = (text, titleExp, viewExp) = > {const [string1, string2, string3] = [... text]; return `{string1} ${titleExp} ${string2} ${viewExp} ${string3}`; const intro = detail` the title of this article is "${title}" and the current readings are: ${view} `; console.log (intro) / / the title of the article is "JavaScript string skills". The current number of readings is: 300004. Verify that there are substrings in the string array

It's easy to find out if there is a substring in a JavaScript string. In ES6, you only need to use the includes function.

However, you need to verify whether the string is stored in the data. If one of the items in the main array contains, true is returned. If none of them contains the return false, you need to use the some function with includes, as shown in the following code:

Const arrayTitles = ["Javascript", "EScript", "Golang"]; const hasText = (array, findText) = > array.some ((item) = > item.includes (findText)); console.log (hasText (arrayTitles, "script")); / / trueconsole.log (hasText (arrayTitles, "php")); / / false "JavaScript how to verify whether there are substrings in the string array" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report