0
var string = "Administration for administering str1234 as an administrator";

I want to get the "str1234" only from the string variable. How do I do this?

Note that the index of substring "str1234" is not always consistent.

4
  • can you please post complete code,where this string sets? Commented Jun 20, 2018 at 8:09
  • Use substring to get the string Commented Jun 20, 2018 at 8:10
  • 1
    @mbadeveloper is saying that "index of substring "str1234" is not always consistent" Commented Jun 20, 2018 at 8:10
  • Does the string follow a regular pattern (ie. can you use a regular expression)? By index of substring, do you mean that the end is not always consistent, or the start is not always consistent? (ie. are said strings always in the format "Administration for administering x as an administrator") Commented Jun 20, 2018 at 8:24

6 Answers 6

1

You can use Regular Expressions to search for patterns in strings.

To search for the pattern str followed by 1-4 numbers you would use the following expression:

\bstr\d{1,4}\b

Explanation of RegExp by regex101.com

\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
str matches the characters str literally (case sensitive) .
\d{1,4} matches a digit (equal to [0-9]) .
{1,4} Quantifier — Matches between 1 and 4 times, as many times as possible, giving back as needed (greedy) .
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

In JavaScript:

let input = 'Administration for administering str1234 as an administrator';
let match = input.match(/\bstr\d{1,4}\b/);

The object match can be used like this:

match[0]    // "str1234"
match.index // 33
match.input // "Administration for administering str1234 as an administrator"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @dislick. This is almost somehow what I'm looking for. I tested your suggestion and found out that it only matches a word with an 'str' at the beginning using \b. So if I have a case like this: var input = 'Administration for administering costr1234 as an administrator'; Thus, it returned null. Sometimes the word with 'str' from the string does not always begins with 'str' like the one above (costr1234).
Also, I also have cases like this. var input2 = 'Administration for administering costr1234 and comSTR5242 as an administrator in _STr4324'; I want to get all 'str1234', 'STR5242' and 'STr4324' and store it in an array. I will greatly appreciate your help in doing this!
Okay @Roger, try this: regex101.com/r/d9Wy3U/1. On the left side there is a "Code Generator" that you can use to see how you would use group matches in JavaScript.
0

Use indexof()

var string = "Administration for administering str1234 as an administrator";

var index; index= string.indexOf('str1234')
console.log(index)

To change 'str1234' to someother value ,you can use replace() .

Hope this helps :)

1 Comment

OP needs to get that string followed by number which is not known.
0

You can use a regular expression as follows:

var string = "Administration for administering str1234 as an administrator";

var x = string.match(/str[\d]+/)
console.log(x)

Comments

0

var string = "Administration for administering str1234 as an administrator";
var array = string.split(" ");
cont neededString;
for (const s of array) {
  if(s.startsWith('str')){
	neededString = s;
	break;
 }
//or
if(isAlphaNumeric(s))
{
neededString = s;
	break;
}
}

public boolean isAlphaNumeric(String s){
    String pattern= "^[a-zA-Z0-9]*$";
    return s.matches(pattern);
}

1 Comment

Snippet throwing error - Uncaught SyntaxError: Unexpected identifier
0

You can use substr function

var text = "Administration for administering str1234 as an administrator";
var textSearch = "str1234";
console.log(text.substr(text.indexOf(textSearch),textSearch.length));

Comments

0

You can also do it with indexOf() function by the following snippet:

var string = "Administration for administering str1234 as an administrator";
var subString = "str1234";
if (string.indexOf(subString) >= 0) {
  alert(true); //You can add your functionality here.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.