• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4859
  • Last Modified:

JavaScript and startsWith()

I need to check if a string starts with the word "page" in the javascript section of a jsp-file. Depending on the number of page the user is browsing, it'll be "page 1", "page 2" and so on. So I need to compare the static part of the string, which is "page".
Do I need to use regular expressions for this?

The method String.startsWith() is not working with javascript, or am I wrong in this point?
Thanks for any help.
0
steinchen84
Asked:
steinchen84
  • 2
  • 2
2 Solutions
 
HonorGodCommented:
Regular expressions would certainly be easy.


var data = 'page 1'
if ( /^page (\d+)/.test( data ) ) {
  alert( RegExp.$1 )
} else {
  alert( 'no' )
}

Open in new window

0
 
BadotzCommented:
var data = 'page 1';
var pos = data.indexOf('page');

// pos will = 0 for a match, -1 for failure
0
 
steinchen84Author Commented:
Thank you - both solutions worked fine.
0
 
BadotzCommented:
No worries - glad to help.
0
 
HonorGodCommented:
Glad to be of some assistance.  Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now