We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

JavaScript and startsWith()

steinchen84
steinchen84 asked
on
Medium Priority
4,916 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

Software Engineer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2007
Commented:
var data = 'page 1';
var pos = data.indexOf('page');

// pos will = 0 for a match, -1 for failure

Author

Commented:
Thank you - both solutions worked fine.
Top Expert 2007

Commented:
No worries - glad to help.
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
Glad to be of some assistance.  Thanks for the grade & points.

Good luck & have a great day
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.