Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get substring from string javascript

Posted on 2009-04-01
4
Medium Priority
?
703 Views
Last Modified: 2012-05-06
Hi, I have this string: http://msn.com/news/Stories/65789?id=12&type=software

How to get 65789??
http://msn.com/news/Stories/65789?id=12&type=software

Open in new window

0
Comment
Question by:Saroj13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Expert Comment

by:k_murli_krishna
ID: 24042506
Please refer:
JavaScript substring() Method
http://www.w3schools.com/jsref/jsref_substring.asp
JavaScript - Strings
http://www.quirksmode.org/js/strings.html
Javascript: substr() v.s. substring() « Rudimentary Art of ...
http://rapd.wordpress.com/2007/07/12/javascript-substr-vs-substring/
0
 
LVL 12

Expert Comment

by:alien109
ID: 24042526
var str = "http://msn.com/news/Stories/65789?id=12&type=software";
var regEx = /([0-9]+)/;
var match = regEx.exec(str)[0];
0
 
LVL 5

Expert Comment

by:prokvk
ID: 24042531
i.e. like this:



var str = 'http://msn.com/news/Stories/65789?id=12&type=software';
            
            var reg = new RegExp('(.*)Stories\/([0-9]+)\?(.*)','i');
            
            var val = str.replace(reg,'$2');
            
            alert(val);

Open in new window

0
 
LVL 11

Accepted Solution

by:
petiex earned 2000 total points
ID: 24042694
Or, you could just use string functions
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <script type="text/javascript">
        <!--
        function testFunction(str){
         alert(getStringBetweenLastSlashAndQuestionMark(str)+" should be 65789");
        }
        function getStringBetweenLastSlashAndQuestionMark(str){
            return str.substring(str.lastIndexOf("/")+1,str.indexOf("?"));
        }
                -->
    </script>
</head>
<body>
<a href="#" onclick="javascript:testFunction('http://msn.com/news/Stories/65789?id=12&type=software'); return false;">click</a>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question