Javascript: Searching substring pattern and returning found string

javascript string contains
javascript substring
javascript string contains character
indexof javascript
javascript regex
javascript string contains letters
javascript string replace
string contains w3school

I have a string composed by several fields. Two of which will always vary in length. I could simply use substring if all fields have fixed lengths.



The fields are divided like this:

480 | 01 | MCAbastillas | 2200800046300017 | 100518 | 110555 | 130000 | 123

The bolded fields are the fields that varies in length. They represent a name and a amount, respectively. I already posted this question but I mistakenly tagged it with Java. I tried to interpret the provided answers into Javascript but being no expert in it, I spent all day producing no results :(

You can use regular expression to capture the groups

const regex = /^(.{3})(.{2})(\D+)(.{16})(.{6})(.{6})(\d+)(.{3})$/
const str = '48001MCAbastillas2200800046300017100518110555130000123'
const values = str.match(regex)

How to Search Strings in JavaScript, Covers JavaScript methods for searching strings: indexOf, lastIndexOf, search, The first three return the location in the string of the match, or -1 if no match is found. JavaScript's string search method accepts a single argument: a pattern to  A function to be invoked to create the new substring to be used to replace the matches to the given regexp or substr. The arguments supplied to this function are described in the "Specifying a function as a parameter" section below. Return value. A new string, with all matches of a pattern replaced by a replacement. Description

var input = '48001MCAbastillas2200800046300017100518110555130000123';
// match parts with regex
var match = input.match(/^(.{3})(.{2})([a-zA-Z]+)(.{16})(.{6})(.{6})(\d+)(.{3})$/);
// remove first element (full matching input)
// build output
var output = match.join(' | ');

JavaScript String search() Method, The search value can be string or a regular expression. This method returns -1 if no match is found. Read more about regular expressions in our RegExp  The search() method searches a string for a specified value, and returns the position of the match. The search value can be string or a regular expression. This method returns -1 if no match is found. Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.

const test = [

const p = /(?<name>\D+)(\d{28})(?<amount>\d+)(\d{3}$)/

console.log ( (s => s.match (p).groups))

How to Check Whether a String Contains a Substring in JavaScript, This method returns the index or position of the first occurrence of substring within the string, otherwise, it returns -1 if no match found. Further, you can use the search() method to search a particular piece of text or pattern (using regular  The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string between "start" and "end", not including "end" itself.

A more complex version that breaks all the parts based on known values. The regex answer is the way to go.

var r1 = "48001MCAbastillas2200800046300017100518110555130000123";
var r2 = "48001RANDOM220080004630001710051811055512345123";

function extract(str) {
    var i = 5,max = str.length;
    var parts = [], cut1, cut2 = (max - 3);
    for(;i<max;i++) {
        if (! isNaN(parseInt(str[i]))) {
            cut1 = i;
    parts.push(str.substr(5,(cut1 - 5)));
    parts.push(str.substr((cut1 + 16),6));
    parts.push(str.substr((cut1 + 16 + 6),6));
    parts.push(str.substr((cut1 + 16 + 12),(cut2 - (cut1 + 16 + 12))));
    return parts;


JavaScript: Check if String Contains a Substring, Checking if a String contains a substring is a common operation and slightly differs As you can see, a boolean value is returned since the string "stack" is a substring of index position of the substring, and a -1 when the substring wasn't found. Checking for substrings in a string with regex can be achieved using the​  many thanks. seems like my JS code isn't working though. here i'm trying to use this search function, in order to append new text to the array element after which corresponds with the search. the html code used for the array is here. nothing happens when click the button, and the problem is with var 't' somehow. if u comment lines 19,20 at least the button will output the code. but from some

Determining if a String Contains a Substring in JavaScript, Its job is to return the index at which a given substring is found. In the event that no match is found, it will return -1 . That means that we can use it, but the clarity  AFAIK, both substring() and indexOf() are supported by both Mozilla and IE. However, note that substr() might not be supported on earlier versions of some browsers (esp. Netscape/Opera). Your post indicates that you already know how to do it using substring() and indexOf(), so I'm not posting a code sample.

Methods of RegExp and String, The method str.match(regexp) finds matches for regexp in the string str . It has 3 It returns an iterable object with matches instead of an array. We can make We can use it without regexps, to search and replace a substring:. The index of the search at which the result was found. input A copy of the search string. Description. If the regular expression does not include the g flag, str.match() will return the same result as RegExp.exec(). Other methods. If you need to know if a string matches a regular expression RegExp, use RegExp.test()., Description. When you want to know whether a pattern is found, and also know its index within a string, use search() . (If  Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), search(), and split() methods of String. This chapter describes JavaScript regular expressions.

  • The not bolded strings will be always the same?
  • So do you want to split the first string into the components you've given or do you just want to get the bold items you've listed from the string
  • The will vary in value but the length will always be the same
  • Very little information to go on. Can you confirm that the 6th character is always the start of a random length of letters until another digit is reached?
  • @Uzi what are the allowed characters for the bold parts ?
  • I think this is the more accurate solution
  • I just have to ignore the first element of the array since it returns the original string followed by the separated set. Thanks. This works for me.
  • Yes, the first element is the string that matches the regular expression.